hello,
i've just now started studying threads.i hav problem with synchronization & sleep method.
what does mean ' a
thread holds the lock of the object '(in synchronization)
does it mean that no other thread can enter the sync. method unless 1st one leaves the lock.
i hav also read that, sleep method doesnt relinquish the lock the thread might have.
so i've tried the program to get o/p as follows:
one:0//with sleep(200) between each line of print
one:1//
..
..
one:5
two:0
two:1
..
..
two:5
but i didnt get this by following program ,plz tell me corrections.
class MyThread extends Thread{
MyThread(
String a){
super(a);
start();
}
public void run(){
SS();
}
synchronized void SS(){
for(int i=0;i<6;i++){
System.out.println(getName()+" : "+i);
try{sleep(500);}
catch(Exception e){System.out.println(getName()+":interrupted!");}
}
}
}
class ThreadDemo{
public ThreadDemo(){
MyThread t1 = new MyThread("one");
MyThread t2 = new MyThread("two");
}
public static void main(String asd[]){
new ThreadDemo();
}
}
-------------