Monday, March 20, 2017

java thread & synchronized example

package threadjoinexample;

import java.util.logging.Level;
import java.util.logging.Logger;

public class ThreadJoinExample {

    public static void main(String[] args) {
        Table mTable = new Table();//only one object 
       
        Thread t1 = new Thread(new MyRunnable(mTable), "1st");
        Thread t2 = new Thread(new MyRunnable(mTable), "2nd");
        Thread t3 = new Thread(new MyRunnable(mTable), "3rd");

        t1.start();

        //start second thread after waiting for 2 seconds or if it's dead
//        try {
////            t1.join(2000);
//            System.out.println("1st try");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        try {
            t1.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadJoinExample.class.getName()).log(Level.SEVERE, null, ex);
        }
        t2.start();

        //checks if this thread is alive
        System.out.println(t1.getName() + "=, isAlive = " + t1.isAlive());

        //start third thread only when first thread is dead
//        try {
////            t1.join();
//            System.out.println("2nd block  try="+t1.getName());
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        try {
            t2.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadJoinExample.class.getName()).log(Level.SEVERE, null, ex);
        }
        t3.start();
       
        try {
            t3.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(ThreadJoinExample.class.getName()).log(Level.SEVERE, null, ex);
        }

        //let all threads finish execution before finishing main thread
//        try {
//
////            t1.join();
//
//            System.out.print(t2.getName());
//            //checks if this thread is alive
//            System.out.println(", t2 isAlive = " + t2.isAlive());
//
////            t2.join();
////            t3.join();
//        } catch (InterruptedException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }

        System.out.println("All threads are dead, exiting main thread");
    }

}

class MyRunnable implements Runnable {
    Table t;
   
    MyRunnable(Table t) {
        this.t = t;
    }

    synchronized void test() {
        for (int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.println(i*100 + " " + Thread.currentThread().getName());
        }
    }

    @Override
    public void run() {
        System.out.println("--Thread started:::" + Thread.currentThread().getName());
        t.printTable(100);
//        test();
//        try {
//            Thread.sleep(4000);
//            System.out.println("run() 4s after=" + Thread.currentThread().getName());
//
//            test();
//
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("!--Thread ended:::" + Thread.currentThread().getName()+"\n\n");
    }

}

class Table {

    synchronized void printTable(int n) {//synchronized method 
        for (int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.println(i*100 + " " + Thread.currentThread().getName());
        }

    }
}

No comments:

Post a Comment