giri
giri

Reputation: 27199

threading concepts in java

I am intermediate in java. I am working with a company quite new to company, they have asked me to take session on "threading concepts in java with real example" as I don't have hands on on threading I can just prepare slides hoe threads can be implemented using Thread class or Runnable interface.

Can anybody please help me out with real scenario of threads and its implementation? Thanks in advance

Upvotes: 2

Views: 2602

Answers (4)

duffymo
duffymo

Reputation: 308743

I would recommend Brien Goetz' "Java Concurrency In Practice". It talks about features added to the JDK above and beyond Thread and Runnable that will make your life better.

In 2016 it's a better idea to dig into the java.util.concurrency package and JDK 8 lambdas and parallel stream. No one should be trying to write multithreaded code with raw Thread unless they know what they're doing. We've been given better abstractions - use them.

Upvotes: 6

ultimate cause
ultimate cause

Reputation: 2294

A real life example can be a telecom application SCP (service control point), that receives lot of requests , (in order of 400 in a sec). The application that handles the request employs a master-slave configuration. There is a thread pool, each thread of which is waiting for a signal to run.

The master thread, receives the request, the request data is posted in some object, that the thread functions reads, and then the thread is signaled to run. When the processing is finished the worker thread is returned to the thread pool.

There can be a flag, that informs about the status of the thread for example, busy, idle, bad etc.

Upvotes: 1

Vivin Paliath
Vivin Paliath

Reputation: 95508

I'd probably start with Sun's (now Oracle's) excellent documentation on concurrency.

As an example, you can create something like a banking application where you have a shared data structure (accounts), and you have multiple threads operating on the account (performing withdrawals and deposits).

Upvotes: 3

Aravind Yarram
Aravind Yarram

Reputation: 80176

The questions is too generic to answer but here are few details. Threads are used to run multiple things in parallel (in theory only it is based on lot of other factors like num of cpus, no of cores etc etc). Multi-threading has gone through lot of improvements in JDK since its inception. You can read the tutorial here: http://download.oracle.com/javase/tutorial/essential/concurrency/

Upvotes: 1

Related Questions