I make this thread for my understanding about threads. During my study i found that thread is typical subject to understand. so take care your mind while you are executing thread example. because in every run you get different result.
Example 1) Simple Threads. Single Task Per Thread
package com.myapp.thread;
import java.util.ArrayList;
import java.util.List;
public class ThreadTest {
// "Runnable" is the task to perform. The Thread is the worker who is doing
// this task.
public static void main(String[] args) {
// We will store the threads so that we can check if they are done
List<Thread> threads = new ArrayList<Thread>();
// We will create 500 threads
for (int i = 0; i < 500; i++) {
Runnable task = new MyRunnable(10000000L + i);
Thread worker = new Thread(task);
// We can set the name of the thread
worker.setName(String.valueOf(i));
// Start the thread, never call method run() direct
worker.start();
// Remember the thread for later usage
threads.add(worker);
}
System.out.println("total thread " + threads.size());
int running = 0;
do {
running = 0;
for (Thread thread : threads) {
if (thread.isAlive()) {
running++;
}
}
System.out.println("We have " + running + " running threads. ");
} while (running > 0);
}
}
/**
* MyRunnable will count the sum of the number from 1 to the parameter
* countUntil and then write the result to the console.
* <p>
* MyRunnable is the task which will be performed
*
* @author siddhartha
*
*/
class MyRunnable implements Runnable {
private final long countUntil;
MyRunnable(long countUntil) {
this.countUntil = countUntil;
}
public void run() {
long sum = 0;
for (long i = 1; i < countUntil; i++) {
sum += i;
}
System.out.println(sum);
}
}
Example 2) Multiple Thread per Task
package com.myapp.thread;
public class MultiThreadImplementRunnableWithoutSynchronized {
public static void main(String args[]) {
MyClass myClass = new MyClass();
Thread thread1 = new Thread(myClass);
thread1.setName(" thread1 ");
Thread thread2 = new Thread(myClass);
thread2.setName(" thread2 ");
thread1.start();
thread2.start();
boolean thread1IsAlive = true;
boolean thread2IsAlive = true;
do {
if (thread1IsAlive && !thread1.isAlive()) {
thread1IsAlive = false;
System.out.println("Thread 1 is dead.");
}
if (thread2IsAlive && !thread2.isAlive()) {
thread2IsAlive = false;
System.out.println("Thread 2 is dead.");
}
} while (thread1IsAlive || thread2IsAlive);
}
}
class MyClass implements Runnable {
static String message[] = { "Java", "is", "hot,", "aromatic,", "and",
"invigorating." };
public void run() {
for (int i = 0; i < message.length; ++i) {
randomWait();
System.out.println(Thread.currentThread().getName() + message[i]);
}
}
void randomWait() {
try {
Thread.currentThread().sleep((long) (3000 * Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
}
}
}
Example 3.) Synchronized the multiple threads while doing same work
package com.myapp.thread;
public class MultiThreadSynchronization {
public static void main(String args[])
{
MyThread1 thread1 = new MyThread1("thread1: ");
MyThread1 thread2 = new MyThread1("thread2: ");
thread1.start();
thread2.start();
boolean thread1IsAlive = true;
boolean thread2IsAlive = true;
do {
if (thread1IsAlive && !thread1.isAlive()) {
thread1IsAlive = false;
System.out.println("Thread 1 is dead.");
}
if (thread2IsAlive && !thread2.isAlive()) {
thread2IsAlive = false;
System.out.println("Thread 2 is dead.");
}
} while(thread1IsAlive || thread2IsAlive);
}
}
class MyThread1 extends Thread
{
static String message[] =
{ "Java", "is", "hot,", "aromatic,", "and", "invigorating."};
public MyThread1(String id)
{
super(id);
}
public void run()
{
SynchronizedOutput.displayList(getName(),message);
}
void randomWait()
{
try {
sleep((long)(3000*Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
}
}
}
class SynchronizedOutput
{
public static synchronized void displayList(String name,String list[])
{
for(int i=0;i<list.length;++i) {
MyThread1 t = (MyThread1) Thread.currentThread();
t.randomWait();
System.out.println( name+ list[i]);
}
}
}
Example 4) Thread Interference
package com.myapp.thread;
public class ThreadInterference implements Runnable{
int c = 0;
public void increment() {
c++;
}
public void decrement() {
c--;
}
public int value() {
return c;
}
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+" "+value());
increment();
System.out.println(Thread.currentThread().getName()+" "+value());
decrement();
System.out.println(Thread.currentThread().getName()+" "+value());
}
public static void main(String[] args) {
ThreadInterference threadInterference = new ThreadInterference();
Thread t1=new Thread(threadInterference);
t1.setName("t1");
t1.start();
Thread t2=new Thread(threadInterference);
t2.start();
t2.setName("t2");
Thread t3=new Thread(threadInterference);
t3.start();
t3.setName("t3");
}
}
Example 5) ThreadLocal Example. Its Another method to create Thread
package com.myapp.thread;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ThreadLocalExample {
private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>() {
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyyMMdd HHmm");
}
};
public String formatIt(Date date) {
return formatter.get().format(date);
}
public static void main(String[] args) {
ThreadLocalExample example=new ThreadLocalExample();
System.out.println(example.formatIt(new Date()));
}
}
Example 6) Java 6 Callable Interface example by which threads can give u result after completion of work
package com.myapp.thread;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class ThreadWithCallableInterfaceTest {
private static final int NTHREDS = 10;
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
List<Future<Long>> list = new ArrayList<Future<Long>>();
for (int i = 0; i < 20000; i++) {
Callable<Long> worker = new MyCallable();
Future<Long> submit = executor.submit(worker);
list.add(submit);
}
long sum = 0;
System.out.println(list.size());
// Now retrieve the result
for (Future<Long> future : list) {
try {
sum += future.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
System.out.println(sum);
executor.shutdown();
}
}
package com.myapp.thread;
import java.util.concurrent.Callable;
public class MyCallable implements Callable<Long>{
public Long call() throws Exception {
long sum = 0;
for (long i = 0; i <= 100; i++) {
sum += i;
}
return sum;
}
}
Example 7) Java 6 Executor Interface Example in threads
package com.myapp.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadWithExecutorInterfaceExample {
private static final int NTHREDS = 10;
//The executor framework presented in this chapter works with Runnables. Runnable do not return result.
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
for (int i = 0; i < 5; i++) {
Runnable worker = new MyRunnable(1000L + i);// MyRunnable is same as in example 1)
executor.execute(worker);
}
// This will make the executor accept no new threads
// and finish all existing threads in the queue
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
Example 8) Non Blocking API in Java 6
package com.myapp.thread;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
public class NonBlockingCompareAndSwap {
private static final int NTHREDS = 10;
public static void main(String[] args) {
final Counter counter = new Counter();
List<Future<Integer>> list = new ArrayList<Future<Integer>>();
ExecutorService executor = Executors.newFixedThreadPool(NTHREDS);
for (int i = 0; i < 500; i++) {
Callable<Integer> worker = new Callable<Integer>() {
public Integer call() throws Exception {
int number = counter.increment();
System.out.println(number );
return number ;
}
};
Future<Integer> submit= executor.submit(worker);
list.add(submit);
}
// This will make the executor accept no new threads
// and finish all existing threads in the queue
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
Set<Integer> set = new HashSet<Integer>();
for (Future<Integer> future : list) {
try {
set.add(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
if (list.size()!=set.size()){
throw new RuntimeException("Double-entries!!!");
}
}
}
class Counter {
private AtomicInteger value = new AtomicInteger();
public int getValue(){
return value.get();
}
public int increment(){
return value.incrementAndGet();
}
// Alternative implementation as increment but just make the
// implementation explicit
public int incrementLongVersion(){
int oldValue = value.get();
while (!value.compareAndSet(oldValue, oldValue+1)){
oldValue = value.get();
}
return oldValue+1;
}
//The interesting part is how incrementAndGet() is implemented. It uses a CAS operation.
/* public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
*/
}
Example 9) Java 7 Fork and Join Example
package com.myapp.thread;
import java.util.Arrays;
import java.util.Random;
import jsr166y.forkjoin.ForkJoinExecutor;
import jsr166y.forkjoin.ForkJoinPool;
import jsr166y.forkjoin.RecursiveAction;
public class ForkJoinFrameworkTestJava7 {
// Java 7 introduce a new parallel mechanism for compute intensive tasks,
// the fork-join framework. The fork-join framework allows you to distribute
// a certain task on several workers and then wait for the result.
// If you are using Java 6.0 you can download the package (jsr166y) from Oracle Website and can test the //example
// For testing create the Java project
//If you are not using Java 7 you also need to "jsr166y.jar" to the classpath with java 6. Java7 need 62 byte //OS
public static void main(String[] args) {
Problem test = new Problem();
// Check the number of available processors
int nThreads = Runtime.getRuntime().availableProcessors();
System.out.println(nThreads);
Solver mfj = new Solver(test.getList());
ForkJoinExecutor pool = new ForkJoinPool(nThreads);
pool.invoke(mfj);
long result = mfj.result;// i have to check this line
System.out.println("Done. Result: " + result);
long sum = 0;
// Check if the result was ok
for (int i = 0; i < test.getList().length; i++) {
sum += test.getList()[i];
}
System.out.println("Done. Result: " + sum);
}
}
class Problem {
private final int[] list = new int[2000000];
public Problem() {
Random generator = new Random(19580427);
for (int i = 0; i < list.length; i++) {
list[i] = generator.nextInt(500000);
}
}
public int[] getList() {
return list;
}
}
class Solver extends RecursiveAction {
private int[] list;
public long result;
public Solver(int[] array) {
this.list = array;
}
@Override
protected void compute() {
if (list.length == 1) {
result = list[0];
} else {
int midpoint = list.length / 2;
int[] l1 = Arrays.copyOfRange(list, 0, midpoint);
int[] l2 = Arrays.copyOfRange(list, midpoint, list.length);
Solver s1 = new Solver(l1);
Solver s2 = new Solver(l2);
forkJoin(s1, s2);
result = s1.result + s2.result;
}
}
}
Example 10) Defensive Copy and write while running within threads. Because while coping another thread may change the data so defensive copy is good practice while working with threads
package com.myapp.thread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class DefensiveCopyWhileReturningToThread {
List<String> list = new ArrayList<String>();
public void add(String s) {
list.add(s);
}
/**
* Makes a defensive copy of the List and return it This way cannot modify
* the list itself
*
* @return List<String>
*/
public List<String> getList() {
// this below line give you readble list that can't be modified in
// future
return Collections.unmodifiableList(list);// same as Arrays.asList(a)
// this method also give u
// readable list that you
// can't change
}
public static void main(String[] args) {
DefensiveCopyWhileReturningToThread obj = new DefensiveCopyWhileReturningToThread();
obj.add("10");
obj.add("11");
obj.add("12");
obj.add("13");
obj.add("14");
obj.add("15");
obj.add("16");
List MyUnModifiableList = obj.getList();
for (Iterator iterator = MyUnModifiableList.iterator(); iterator
.hasNext();) {
String type = (String) iterator.next();
System.out.println(type);
}
// you can not add new value to unmodifiableList list it throws
// java.lang.UnsupportedOperationException
MyUnModifiableList.add("23");
}
}
Example 10) Defensive Copy and write while running within threads. Because while coping another thread may change the data so defensive copy is good practice while working with threads
package com.myapp.thread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class DefensiveCopyWhileReturningToThread {
List<String> list = new ArrayList<String>();
public void add(String s) {
list.add(s);
}
/**
* Makes a defensive copy of the List and return it This way cannot modify
* the list itself
*
* @return List<String>
*/
public List<String> getList() {
// this below line give you readble list that can't be modified in
// future
return Collections.unmodifiableList(list);// same as Arrays.asList(a)
// this method also give u
// readable list that you
// can't change
}
public static void main(String[] args) {
DefensiveCopyWhileReturningToThread obj = new DefensiveCopyWhileReturningToThread();
obj.add("10");
obj.add("11");
obj.add("12");
obj.add("13");
obj.add("14");
obj.add("15");
obj.add("16");
List MyUnModifiableList = obj.getList();
for (Iterator iterator = MyUnModifiableList.iterator(); iterator
.hasNext();) {
String type = (String) iterator.next();
System.out.println(type);
}
// you can not add new value to unmodifiableList list it throws
// java.lang.UnsupportedOperationException
MyUnModifiableList.add("23");
}
}