多线程复习简单应用

新建一个线程:

可以用类继承Thread类的方法,也可以用class继承Runnable接口的方法,本次用的是使用后者

 1 public class thread{
 2     public static void main(String[] args){
 3         run1 r = new run1();
 4         Thread t = new Thread(r);
 5         t.start();
 6         
 7         for(int i=0;i<100;i++){
 8             System.out.println("main");
 9         }
10     }
11 }
12 
13 class run1 implements Runnable{
14     public void run(){
15         for(int i=0;i<100;i++){
16             System.out.println("run1");
17         }
18     }
19 }

运行结果:

可以看到run方法和买main方法分时间片运行,到此简单的多线程就完成了

接下来是Thread的sleep()方法的一个小应用:

sleep method
 1 public class sleep{
 2     public static void main(String[] args){
 3         run2 r = new run2();
 4         Thread t = new Thread(r);
 5         t.start();
 6         
 7         try{
 8             Thread.sleep(10000);
 9         }catch(InterruptedException e){
10             e.printStackTrace();
11         }
12         r.setFlag(false);
13     }
14 }
15 
16 class run2 implements Runnable{
17     boolean flag = true;
18     
19     public void run(){
20         int t = 0;
21         while(flag == true){
22             if(t<=10){
23                 t++;
24                 try{
25                     Thread.sleep(1000);
26                 }catch(InterruptedException e){
27                     e.printStackTrace();
28                 }
29                 System.out.println("t = " + t);
30             }
31             
32         }
33         
34     }
35     
36     public void setFlag(boolean b){
37         this.flag = b;
38     }
39 }

run方法中如果flag等于true则每个一秒钟,t+1,知道主程序中的sleep()方法执行完(10秒)

这个可以当成定时器用。

编写线程死锁程序

View Code
 1 public class deadlock implements Runnable{
 2     int flag = 0;
 3     static Object o1 = new Object();
 4     static Object o2 = new Object();
 5     
 6     public static void main(String[] args){
 7         deadlock d1 = new deadlock();
 8         deadlock d2 = new deadlock();
 9         
10         Thread t1 = new Thread(d1);
11         Thread t2 = new Thread(d2);
12         
13         d1.flag = 0;
14         d2.flag = 1;
15         
16         t1.start();//t1线程启动,锁定o1 ,t1睡眠,此时t2锁定o2 然后t2睡眠,t1醒来要锁定o2
17         t2.start();//可是o2被t2锁定,t2醒来要锁定o1可是o1被t1锁定,死循环,死锁
18         
19     }
20     
21     
22     public void run(){
23         if(flag == 0){
24             synchronized(o1){//锁定o1
25                 System.out.println("synchronized o1");
26                 try{    //让线程睡眠使两一个线程有执行的机会
27                     Thread.sleep(100);
28                 }catch(InterruptedException e){
29                     e.printStackTrace();
30                 }
31                 
32                 synchronized(o2){//在锁定o1的情况下锁定o2
33                     System.out.println("synchronized o2");
34                     try{    //让线程睡眠使两一个线程有执行的机会
35                         Thread.sleep(100);
36                     }catch(InterruptedException e){
37                         e.printStackTrace();
38                     }
39                 }
40             }
41         }
42         
43         if(flag == 1){
44             synchronized(o2){//锁定o2
45                 System.out.println("synchronized o2");
46                 try{    //让线程睡眠使两一个线程有执行的机会
47                     Thread.sleep(100);
48                 }catch(InterruptedException e){
49                     e.printStackTrace();
50                 }
51                 
52                 synchronized(o1){//在锁定o1的情况下锁定o1
53                     System.out.println("synchronized o1");
54                     try{    //让线程睡眠使两一个线程有执行的机会
55                         Thread.sleep(100);
56                     }catch(InterruptedException e){
57                         e.printStackTrace();
58                     }
59                 }
60             }
61         }
62     }
63 }

结果:

程序执行到一半死锁

posted @ 2013-04-20 23:00  拙急鸟  阅读(219)  评论(0编辑  收藏  举报