Java Day 13

线程的状态
 被创建 运行 冻结 消亡
 
 被创建--start()--> 运行
 运行----run()----> 消亡
         stop()
 运行---sleep(time)---> 冻结
        wait()-->
        <--notify()
 
 cpu 执行资格 被cup处理、处于队列中
 cpu 执行权   正在被cpu处理

创建线程的第二种方法
 实现Runnable接口
 
 Thread t1 = new Thread(d)//d是实现Runnable接口类创建的对象
 1、定义类实现Runnable接口
 2、覆盖接口中的run方法,将线程任务封装
 3、通过Thread类创建线程对象,将对象作为线程构造函数的参数进行传递
 4、调用线程对象的start方法

细节
 单独进行封装

线程安全问题
 1、多线程操作共享数据
 2、操作共享数据的线程代码有多条

同步代码块
 将多条操作共享数据的线程代码封装起来,只允许一个线程访问
 synchronized(对象){ 代码 }

同步代码块 利弊
 利 解决了线程的安全问题
 弊 效率降低

 同步的前提 同步中必须有多个线程使用同一个锁

同步函数
 
同步函数的锁
 同步函数使用的锁是this

静态同步函数的锁
 函数所属字节码文件对象
 this.getClass();
 类.class

 1 class Ticket implements Runnable{ //extends Thread
 2     private int num = 100;
 3     Object obj = new Object();
 4     boolean flag = true;
 5     
 6     public void run(){
 7         //Object obj = new Object();
 8         if(flag){
 9             while(true){
10                 show();
11                 if(num==0)
12                     return;
13             }    
14         }else{
15             while(true){
16                 synchronized(this){
17                     if(num>0){
18                         try{
19                             Thread.sleep(10);
20                         }catch(InterruptedException e){
21                         }    
22                     System.out.println(Thread.currentThread().getName()+"...block..."+num--);
23                     }
24             }
25                 if(num==0)
26                     return;
27             //}
28             }            
29         }
30 
31     }
32     
33     public synchronized void show(){
34         if(num>0){
35             try{
36                 Thread.sleep(10);
37             }catch(InterruptedException e){    }    
38             System.out.println(Thread.currentThread().getName()+"...func..."+num--);
39         }
40     }
41 }
42 
43 
44 public class SaleTicketsDemo{
45     public static void main(String[] args){
46         /*Ticket t1 = new Ticket();
47         Ticket t2 = new Ticket();
48         Ticket t3 = new Ticket();
49         Ticket t4 = new Ticket();
50         
51         t1.start();
52         t2.start();
53         t3.start();
54         t4.start();*/
55         Ticket t = new Ticket();
56         Thread t1 = new Thread(t);
57         Thread t2 = new Thread(t);
58         
59         t1.start();
60         try{Thread.sleep(10);}catch(InterruptedException e){    }
61         t.flag = false;
62         t2.start();
63 
64     }
65 }

 

单例模式 的多线程问题
 

 1 class Single{
 2     private static Single s = null;
 3     private Single(){}
 4     public static Single getInstance(){
 5         if(s==null){
 6             synchronized(Single.class){
 7                 if(s==null){
 8                     s = new Single();
 9                 }
10             }
11         }
12         return s;
13     }
14 }


死锁
 1、同步的嵌套

 1 class Test implements Runnable{
 2     private boolean flag;
 3     Test(boolean blag){
 4         this.flag = flag;
 5     }
 6     public void run(){
 7         if(flag){
 8             while(true){
 9                 synchronized(MyLock.locka){
10                     System.out.println(Thread.currentThread().getName()+"  if  locka...");
11                     synchronized(MyLock.lockb){
12                         System.out.println(Thread.currentThread().getName()+"  if  lockb...");
13                     }
14                 }
15             }
16         }else{
17             while(true){
18                 synchronized(MyLock.lockb){
19                     System.out.println(Thread.currentThread().getName()+"  else  lockb...");
20                     synchronized(MyLock.locka){
21                         System.out.println(Thread.currentThread().getName()+"  else  locka...");
22                     }
23                 }
24             }
25         }
26     }
27 }
28 class MyLock{
29     public static final Object locka = new Object();
30     public static final Object lockb = new Object();
31 }
32 class DeadLockTest{
33     public static void main(String[] args){
34         Test a = new Test(true);
35         Test b = new Test(false);
36         
37         Thread t1 = new Thread(a);
38         Thread t2 = new Thread(b);
39         t1.start();
40         t2.start();
41     }
42 }
View Code

 

posted @ 2016-09-25 23:17  zhuzhuqwa  阅读(135)  评论(0编辑  收藏  举报