Java Threading Concept ..

8 Sep

Thread Pool Worker :

=====================

/*In above program, we are creating fixed size thread pool of 5 worker threads.
* Then we are submitting 10 jobs to this pool,
* since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state,
*  as soon as one of the job is finished, another job from
*  the wait queue will be picked up by worker thread and get’s executed.
* */

package com.soumya.test.prep;

/*In above program, we are creating fixed size thread pool of 5 worker threads.
* Then we are submitting 10 jobs to this pool,
* since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state,
*  as soon as one of the job is finished, another job from
*  the wait queue will be picked up by worker thread and get’s executed.
* */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread(“” + i);
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println(“Finished all threads”);
}

}

package com.soumya.test.prep;

public class WorkerThread implements Runnable {

private String command;

public WorkerThread(String s){
this.command=s;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+”Start. Command = “+command);
processCommand();
System.out.println(Thread.currentThread().getName()+” End.”);
}

private void processCommand() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Override
public String toString(){
return this.command;
}
}

Simple DealLockExample :

==========================

package com.soumya.test.prep;

public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format(“%s: %s”
+ ”  has bowed to me!%n”,
this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format(“%s: %s”
+ ” has bowed back to me!%n”,
this.name, bower.getName());
}
}

public static void main(String[] args) {
final Friend alphonse =
new Friend(“Alphonse”);
final Friend gaston =
new Friend(“Gaston”);
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}
}

Advertisements

One Response to “Java Threading Concept ..”

Trackbacks/Pingbacks

  1. [Java] Worker thread – thread-safe processing items one by one | JetCracker - September 8, 2013

    […] Java Threading Concept .. (csesoumya.wordpress.com) […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: