Tuesday, November 29, 2016

3 Threads execution ordering


public class TestSynchronisedNotify {

static Object monitor = new Object();

static int count = 0;



static class MyThread extends Thread {

int name ;


public int getN() {

return name;


}
public void setN(int name) {

this.name = name;


}
public MyThread(int name) {

this.name = name;


}
public void run() {

while (true && count <= 20) {


synchronized (monitor) {


if (count % 3 == this.getN() ) {

System.out.println(this.getN() + " :" + count);

count++;


}


}

}

}

}


public static void main(String[] args) {



Thread t1 = new MyThread(1);

Thread t2 = new MyThread(2);

Thread t3 = new MyThread(0);

t1.start();

t2.start();

t3.start();




}

}
 

No comments:

Post a Comment