This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra). *** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***

Monday, January 25, 2010

21 - example of Thread.join - level basic - Java 1.6.0_17 - NB 6.8

3 classes
Main
ClassA
and ClassB
Main will start 2 threads one of ClassA and one of ClassB.
The threads do a loop and wait some time after each iteration.
After 3 seconds Main will send a message to ClassA (thread A) to join the Thread B.
(ClassA will check an instance variable and if this instance variable is initialized, then it is the thread to join)


package threadjoin;

public class Main {

    public static void main(String[] args) {

        ClassA a = new ClassA();
        ClassB b = new ClassB();

        Thread ta = new Thread(a);
        Thread tb = new Thread(b);

        ta.start();
        tb.start();

        System.out.println("sleeping 3 seconds ! then will force Thread A to join Thread B !");
        Thread main = Thread.currentThread();
        try {
            main.sleep(3000);
            a.threadToJoin = tb;
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }


        System.out.println("after 3 seconds and thread A should join thread B !");
    }
}

package threadjoin;

/**
 *
 * @author Rudy
 */
public class ClassA implements Runnable {

    Thread threadToJoin;
    final static int counter = 100;
    final static int timeToWait = 100000000;

    public void run() {
        for (int c = 0; c < counter; c++) {
            System.out.println("A --- Loop #" + c);
            for (int t = 0; t < timeToWait; t++);
            if (threadToJoin != null) {
                try {
                    System.out.println("Thread A joining Thread B !!!");
                    threadToJoin.join();
                    // deactivation of the test !
                    threadToJoin = null;
                } catch (InterruptedException ie) {
                    ie.printStackTrace();
                }
            }
        }

    }
}

package threadjoin;

/**
 *
 * @author Rudy
 */
public class ClassB implements Runnable {

    final static int counter = 100;
    final static int timeToWait = 100000000;

    public void run() {
        for (int c = 0; c < counter; c++) {
            System.out.println("B --- Loop #" + c);
            for (int t = 0; t < timeToWait; t++);
        }

    }
}

run:
sleeping 3 seconds ! then will force Thread A to join Thread B !
A --- Loop #0
B --- Loop #0
A --- Loop #1
B --- Loop #1
A --- Loop #2
B --- Loop #2
A --- Loop #3
B --- Loop #3
A --- Loop #4
B --- Loop #4
A --- Loop #5
B --- Loop #5
A --- Loop #6
B --- Loop #6
B --- Loop #7
A --- Loop #7
B --- Loop #8
A --- Loop #8
B --- Loop #9
A --- Loop #9
B --- Loop #10
A --- Loop #10
B --- Loop #11
A --- Loop #11
B --- Loop #12
A --- Loop #12
B --- Loop #13
A --- Loop #13
B --- Loop #14
A --- Loop #14
B --- Loop #15
A --- Loop #15
B --- Loop #16
A --- Loop #16
B --- Loop #17
A --- Loop #17
B --- Loop #18
A --- Loop #18
B --- Loop #19
A --- Loop #19
B --- Loop #20
A --- Loop #20
B --- Loop #21
A --- Loop #21
B --- Loop #22
B --- Loop #23
A --- Loop #22
B --- Loop #24
A --- Loop #23
B --- Loop #25
A --- Loop #24
B --- Loop #26
A --- Loop #25
B --- Loop #27
A --- Loop #26
B --- Loop #28
A --- Loop #27
B --- Loop #29
A --- Loop #28
B --- Loop #30
A --- Loop #29
B --- Loop #31
A --- Loop #30
B --- Loop #32
A --- Loop #31
B --- Loop #33
A --- Loop #32
B --- Loop #34
A --- Loop #33
B --- Loop #35
A --- Loop #34
B --- Loop #36
A --- Loop #35
B --- Loop #37
A --- Loop #36
B --- Loop #38
A --- Loop #37
B --- Loop #39
A --- Loop #38
B --- Loop #40
A --- Loop #39
B --- Loop #41
A --- Loop #40
B --- Loop #42
A --- Loop #41
B --- Loop #43
A --- Loop #42
B --- Loop #44
A --- Loop #43
B --- Loop #45
A --- Loop #44
B --- Loop #46
A --- Loop #45
B --- Loop #47
A --- Loop #46
B --- Loop #48
A --- Loop #47
B --- Loop #49
A --- Loop #48
B --- Loop #50
A --- Loop #49
B --- Loop #51
A --- Loop #50
B --- Loop #52
A --- Loop #51
B --- Loop #53
after 3 seconds and thread A should join thread B !
Thread A joining Thread B !!!
B --- Loop #54
B --- Loop #55
B --- Loop #56
B --- Loop #57
B --- Loop #58
B --- Loop #59
B --- Loop #60
B --- Loop #61
B --- Loop #62
B --- Loop #63
B --- Loop #64
B --- Loop #65
B --- Loop #66
B --- Loop #67
B --- Loop #68
B --- Loop #69
B --- Loop #70
B --- Loop #71
B --- Loop #72
B --- Loop #73
B --- Loop #74
B --- Loop #75
B --- Loop #76
B --- Loop #77
B --- Loop #78
B --- Loop #79
B --- Loop #80
B --- Loop #81
B --- Loop #82
B --- Loop #83
B --- Loop #84
B --- Loop #85
B --- Loop #86
B --- Loop #87
B --- Loop #88
B --- Loop #89
B --- Loop #90
B --- Loop #91
B --- Loop #92
B --- Loop #93
B --- Loop #94
B --- Loop #95
B --- Loop #96
B --- Loop #97
B --- Loop #98
B --- Loop #99
A --- Loop #52
A --- Loop #53
A --- Loop #54
A --- Loop #55
A --- Loop #56
A --- Loop #57
A --- Loop #58
A --- Loop #59
A --- Loop #60
A --- Loop #61
A --- Loop #62
A --- Loop #63
A --- Loop #64
A --- Loop #65
A --- Loop #66
A --- Loop #67
A --- Loop #68
A --- Loop #69
A --- Loop #70
A --- Loop #71
A --- Loop #72
A --- Loop #73
A --- Loop #74
A --- Loop #75
A --- Loop #76
A --- Loop #77
A --- Loop #78
A --- Loop #79
A --- Loop #80
A --- Loop #81
A --- Loop #82
A --- Loop #83
A --- Loop #84
A --- Loop #85
A --- Loop #86
A --- Loop #87
A --- Loop #88
A --- Loop #89
A --- Loop #90
A --- Loop #91
A --- Loop #92
A --- Loop #93
A --- Loop #94
A --- Loop #95
A --- Loop #96
A --- Loop #97
A --- Loop #98
A --- Loop #99
BUILD SUCCESSFUL (total time: 9 seconds)

As you can see the Java scheduler will make the thread A and B execute at the same time.
We see

A --- Loop #0
B --- Loop #0
A --- Loop #1
B --- Loop #1
A --- Loop #2
B --- Loop #2
A --- Loop #3
B --- Loop #3
A --- Loop #4
B --- Loop #4
A --- Loop #5
B --- Loop #5
A --- Loop #6
...

Then after 3 seconds A joined B and thus A wait until the end of B.
We see

B --- Loop #54
B --- Loop #55
B --- Loop #56
B --- Loop #57
B --- Loop #58
B --- Loop #59
B --- Loop #60
B --- Loop #61
B --- Loop #62
B --- Loop #63
B --- Loop #64
B --- Loop #65
B --- Loop #66
B --- Loop #67
B --- Loop #68
B --- Loop #69
B --- Loop #70
B --- Loop #71
B --- Loop #72
B --- Loop #73
...
B --- Loop #94
B --- Loop #95
B --- Loop #96
B --- Loop #97
B --- Loop #98
B --- Loop #99

Look A is back !!!

A --- Loop #52
A --- Loop #53
A --- Loop #54
A --- Loop #55
A --- Loop #56
A --- Loop #57
A --- Loop #58
A --- Loop #59
A --- Loop #60
A --- Loop #61
A --- Loop #62
A --- Loop #63
A --- Loop #64
A --- Loop #65
A --- Loop #66
A --- Loop #67
A --- Loop #68
A --- Loop #69
A --- Loop #70
A --- Loop #71
A --- Loop #72
A --- Loop #73
A --- Loop #74
A --- Loop #75
A --- Loop #76
...
A --- Loop #86
A --- Loop #87
A --- Loop #88
A --- Loop #89
A --- Loop #90
A --- Loop #91
A --- Loop #92
A --- Loop #93
A --- Loop #94
A --- Loop #95
A --- Loop #96
A --- Loop #97
A --- Loop #98
A --- Loop #99


- Rudy -


No comments:

Post a Comment

Followers