Posted on 2008-12-06 12:36
鱼跃于渊 阅读(109)
评论(0) 编辑 收藏 所属分类:
j2se
1
2 public class TT1 implements Runnable{
3 int b = 10 ;
4
5 public synchronized void m1(){
6 b = 1000 ;
7 try{
8 Thread.sleep(5000) ;
9 }catch(InterruptedException ex){
10 ex.printStackTrace() ;
11 }
12 System.out.println("m1 : b = " + b) ;
13 }
14
15 public void m2(){
16 b = 2000 ;
17 System.out.println("m2 : b = " + b ) ;
18 }
19
20 public void run(){
21 m1() ;
22 }
23
24 public static void main(String[] args){
25 TT1 tt = new TT1() ;
26 Thread t= new Thread(tt) ;
27 t.start() ;
28 try{
29 Thread.sleep(1000) ;/*que din m1 method was running !*/
30 }catch(InterruptedException ex){
31 ex.printStackTrace() ;
32 }
33 tt.m2() ;
34 }
35 }
result :
m2 : b = 2000
m1 : b = 2000
1
2 public class TT1 implements Runnable{
3 int b = 10 ;
4
5 public synchronized void m1(){
6 b = 1000 ;
7 try{
8 Thread.sleep(5000) ;
9 }catch(InterruptedException ex){
10 ex.printStackTrace() ;
11 }
12 System.out.println("m1 : b = " + b) ;
13 }
14
15 public synchronized void m2(){
16 b = 2000 ;
17 System.out.println("m2 : b = " + b ) ;
18 }
19
20 public void run(){
21 m1() ;
22 }
23
24 public static void main(String[] args){
25 TT1 tt = new TT1() ;
26 Thread t= new Thread(tt) ;
27 t.start() ;
28 try{
29 Thread.sleep(1000) ;/*que din m1 method was running !*/
30 }catch(InterruptedException ex){
31 ex.printStackTrace() ;
32 }
33 tt.m2() ;
34 }
35 }
result:
m1 : b = 1000
m2 : b = 2000