黑马程序员--学习日记7(多线程 2 ) .

-------android培训java培训、期待与您交流! ----------

 

 

//线程间通讯:其实就是多个线程在操作同一个资源,但是
//操作的动作不同。
   //同步锁、、、
 /*  
class Res{
 String name;
 String sex;
}
class Input implements Runnable{
 private Res r;
 Input(Res r){
  this.r = r;
 }
 public void run(){
   int x = 0;
   while(true){
    synchronized(r){
     if(x == 0){
      r.name = "mike";
      r.sex = "man";
     }else{
      r.name = "丽丽";
      r.sex = "女女女女女女";
     }
    }
    
    x = (x+1)%2;
   }
 }
}
class Output implements Runnable{
 private Res r;
 Output(Res r){
  this.r = r;
 }
 public void run(){
   while(true){
    synchronized(r){
     System.out.println(r.name+"...."+r.sex);
    }
    
   }
 }
}
class Thread01{
 public static void main(String args[]){
  Res r = new Res();//要操作的资源
  Input in = new Input(r);//把资源放到要运行的代码上。
  Output out = new Output(r);
  Thread t1 = new Thread(in);//创建线程把要运行的Run代码放到线程上。
  Thread t2 = new Thread(out);
  t1.start();//打开线程。
  t2.start();
 }
}

*/

/*

//代码同步了。
class Res{
 String name;
 String sex;
 boolean flag = false;
}
class Input implements Runnable{
 private Res r;
 Input(Res r){
  this.r = r;
 }
 public void run(){
   int x = 0;
   while(true){
    synchronized(r){
     if(r.flag)
      try{r.wait();}catch(Exception e){}//wait方法只能try不能在Run函数上抛出,因为父类中Run方法,没有抛出,所以子类只能try.
     if(x == 0){
      r.name = "mike";
      r.sex = "man";
     }else{
      r.name = "丽丽";
      r.sex = "女女女女女女";
     }
     r.flag =true;
     r.notify();
    }
    x = (x+1)%2;
   }
 }
}
class Output implements Runnable{
 private Res r;
 Output(Res r){
  this.r = r;
 }
 public void run(){
   while(true){
    synchronized(r){
     if(!r.flag)
      try{r.wait();}catch(Exception e){}
     System.out.println(r.name+"...."+r.sex);
     r.flag = false;
     r.notify();
    }
   }
 }
}
class Thread01{
 public static void main(String args[]){
  Res r = new Res();//要操作的资源
  Input in = new Input(r);//把资源放到要运行的代码上。
  Output out = new Output(r);
  Thread t1 = new Thread(in);//创建线程把要运行的Run代码放到线程上。
  Thread t2 = new Thread(out);
  t1.start();//打开线程。
  t2.start();
 }
}

*/


   //   对上面代码的优化!!!!!!!!!!
   /*
class Res{
 private String name;
 private String sex;
 boolean flag = false;
 public synchronized void set(String name,String sex){
  if(flag)
   try{this.wait();}catch(Exception e){}
  this.name = name;
  //--->该代码有可能出问题。
  this.sex = sex;
  flag = true;
  this.notify();
 }
 public synchronized void out(){
  if(!flag)
   try{this.wait();}catch(Exception e){}
  System.out.println(name+"......"+sex);
  flag = false;
  this.notify();
 }
}
class Input implements Runnable{
 private Res r;
 Input(Res r){
  this.r = r;
 }
 public void run(){
   int x = 0;
   while(true){
    if(x == 0){
     r.set("mike","man");      r.sex = "man";
    }else
     r.set("丽丽","女女女女");  
    x = (x+1)%2;
   }
 }
}
class Output implements Runnable{
 private Res r;
 Output(Res r){
  this.r = r;
 }
 public void run(){
   while(true){
    r.out();
   }
 }
}
class Thread01{
 public static void main(String args[]){
  Res r = new Res();//要操作的资源
  new Thread(new Input(r)).start();
  new Thread(new Output(r)).start();
 }
}

*/


/*         //生产者与消费者问题     单线程
class Thread01{
 public static void main(String args[]){
  Resource r = new Resource();
  Producer pro = new Producer(r);
  Consumer con = new Consumer(r);
  Thread t1 = new Thread(pro);
  Thread t2 = new Thread(con);
  t1.start();
  t2.start();
 }
}
class Resource{
 private String name;
 private int count = 1;
 private boolean flag = false;
 public synchronized void set(String name){
  if(flag)
   try{wait();}catch(Exception e){}
  this.name = name+"**"+count++;
  System.out.println(Thread.currentThread().getName()+"...生产者..*****"+this.name);
  flag = true;
  this.notify();
 }
 public synchronized void out(){
  if(!flag)
   try{wait();}catch(Exception e){}
  System.out.println(Thread.currentThread().getName()+"...消费者.."+this. name);
  flag = false;
  this.notify();
 }
}

class Producer implements Runnable{
 private Resource res;
 Producer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.set("+商品+");
  }
 }
}
class Consumer implements Runnable{
 private Resource res;
 Consumer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.out();
  }
 }
}
*/

 

//    多线程------------ 生产消费者
/*
class Thread01{
 public static void main(String args[]){
  Resource r = new Resource();
  Producer pro = new Producer(r);
  Consumer con = new Consumer(r);
  Thread t1 = new Thread(pro);
  Thread t2 = new Thread(pro);
  Thread t3 = new Thread(con);
  Thread t4 = new Thread(con);
  t1.start();
  t2.start();
  t3.start();
  t4.start();
 }
}
class Resource{
 private String name;
 private int count = 1;
 private boolean flag = false;
 public synchronized void set(String name){
  if(flag)
   try{wait();}catch(Exception e){}
  this.name = name+"**"+count++;
  System.out.println(Thread.currentThread().getName()+"...生产者..*****"+this.name);
  flag = true;
  this.notify();
 }
 public synchronized void out(){
  if(!flag)
   try{wait();}catch(Exception e){}
  System.out.println(Thread.currentThread().getName()+"...消费者.."+this. name);
  flag = false;
  this.notify();
 }
}

class Producer implements Runnable{
 private Resource res;
 Producer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.set("+商品+");
  }
 }
}
class Consumer implements Runnable{
 private Resource res;
 Consumer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.out();
  }
 }
}
*/


/*
class Thread01{
 public static void main(String args[]){
  Resource r = new Resource();
  Producer pro = new Producer(r);
  Consumer con = new Consumer(r);
  Thread t1 = new Thread(pro);
  Thread t2 = new Thread(con);
  t1.start();
  t2.start();
 }
}
class Resource{
 private String name;
 private int count = 1;
 private boolean flag = false;
 public synchronized void set(String name){
  if(flag)
   try{wait();}catch(Exception e){}
  this.name = name+"**"+count++;
  System.out.println(Thread.currentThread().getName()+"...生产者..*****"+this.name);
  flag = true;
  this.notify();
 }
 public synchronized void out(){
  if(!flag)
   try{wait();}catch(Exception e){}
  System.out.println(Thread.currentThread().getName()+"...消费者.."+this. name);
  flag = false;
  this.notify();
 }
}

class Producer implements Runnable{
 private Resource res;
 Producer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.set("+商品+");
  }
 }
}
class Consumer implements Runnable{
 private Resource res;
 Consumer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.out();
  }
 }
}
*/

 

//    多线程------------ 生产消费者----解决方案。while notifyAll
/*
class Thread01{
 public static void main(String args[]){
  Resource r = new Resource();
  Producer pro = new Producer(r);
  Consumer con = new Consumer(r);
  Thread t1 = new Thread(pro);
  Thread t2 = new Thread(pro);
  Thread t3 = new Thread(con);
  Thread t4 = new Thread(con);
  t1.start();
  t2.start();
  t3.start();
  t4.start();
 }
}
class Resource{
 private String name;
 private int count = 1;
 private boolean flag = false;
  // t1  t2
 public synchronized void set(String name){
  while(flag)
   try{wait();}catch(Exception e){}//t1(放弃资格)  t2(获取资格)
  this.name = name+"**"+count++;
  System.out.println(Thread.currentThread().getName()+"...生产者..*****"+this.name);
  flag = true;
  this.notify();
 }
 // t3  t4
 public synchronized void out(){
  while(!flag)
   try{wait();}catch(Exception e){}//t3 (放弃资格) t4(放弃资格)
  System.out.println(Thread.currentThread().getName()+"...消费者.."+this. name);
  flag = false;
  this.notify();
 }
}

class Producer implements Runnable{
 private Resource res;
 Producer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.set("+商品+");
  }
 }
}
class Consumer implements Runnable{
 private Resource res;
 Consumer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.out();
  }
 }
}

*/


/*
class Thread01{
 public static void main(String args[]){
  Resource r = new Resource();
  Producer pro = new Producer(r);
  Consumer con = new Consumer(r);
  Thread t1 = new Thread(pro);
  Thread t2 = new Thread(pro);
  Thread t3 = new Thread(con);
  Thread t4 = new Thread(con);
  t1.start();
  t2.start();
  t3.start();
  t4.start();
 }
}
class Resource{
 private String name;
 private int count = 1;
 private boolean flag = false;
  // t1  t2
  private Lock lock = new ReentrantLock();
  private Condition con  = lock.newConditon();
 public  void set(String name){

  while(flag)
  this.name = name+"**"+count++;
  System.out.println(Thread.currentThread().getName()+"...生产者..*****"+this.name);
  flag = true;
 }
 // t3  t4
 public synchronized void out(){
  while(!flag)
   try{wait();}catch(Exception e){}//t3 (放弃资格) t4(放弃资格)
  System.out.println(Thread.currentThread().getName()+"...消费者.."+this. name);
  flag = false;
  this.notify();
 }
}

class Producer implements Runnable{
 private Resource res;
 Producer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.set("+商品+");
  }
 }
}
class Consumer implements Runnable{
 private Resource res;
 Consumer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.out();
  }
 }
}
*/

 

 

class Exercise{
 public static void main(String args[]){
  Resource r = new Resource();
  Producer pro = new Producer(r);
  Consumer con = new Consumer(r);
  Thread t1 = new Thread(pro);
  Thread t2 = new Thread(pro);
  Thread t3 = new Thread(con);
  Thread t4 = new Thread(con);
  t1.start();
  t2.start();
  t3.start();
  t4.start();
 }
}
class Resource{
 private String name;
 private int count = 1;
 private boolean flag = false;
  // t1  t2
 public synchronized void set(String name){
  while(flag)
   try{wait();}catch(Exception e){}//t1(放弃资格)  t2(获取资格)
  this.name = name+"**"+count++;
  System.out.println(Thread.currentThread().getName()+"...生产者..*****"+this.name);
  flag = true;
  this.notifyAll();
 }
 // t3  t4
 public synchronized void out(){
  while(!flag)
   try{wait();}catch(Exception e){}//t3 (放弃资格) t4(放弃资格)
  System.out.println(Thread.currentThread().getName()+"...消费者.."+this. name);
  flag = false;
  this.notifyAll();
 }
}

class Producer implements Runnable{
 private Resource res;
 Producer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.set("+商品+");
  }
 }
}
class Consumer implements Runnable{
 private Resource res;
 Consumer(Resource res){
  this.res = res;
 }
 public void run(){
  while(true){
   res.out();
  }
 }
}

 

 

 

class Computer{
 //String name;
 int i =0 ;
 boolean flag ;
}
class Made implements Runnable{
 Computer com;
 Made(Computer com){
  this.com = com;
 }
 public void run(){
  while(true){
   synchronized(com){
    if(com.flag){
     try{com.wait();}catch(Exception e){}
    }
    System.out.println(Thread.currentThread().getName()+"  生产电脑: "+(++com.i));
    com.flag = true;
    com.notify();
   }
  }
 }
}
class Move implements Runnable{
 Computer com;
 Move(Computer com){
  this.com = com;
 }
 public void run(){
  while(true){
   synchronized(com){
    if(!com.flag){
     try{com.wait();}catch(Exception e){}
    }
    System.out.println(Thread.currentThread().getName()+"       搬走电脑: "+com.i);
    com.flag = false;
    com.notify();
   }
  }
 }
}
class Exercise{
 public static void main(String args[]){
  Computer com = new Computer();
  Made made = new Made(com);
  Move move = new Move(com);
  Thread t1 = new Thread(made);
  Thread t2 = new Thread(move);
  t1.start();
  t2.start();

 }
}

 

 

 

 

/*
//需求:简单的卖票程序。多个窗口同时买票。
class Ticket implements Runnable{
 //静态的用法:生命调用长。
 private  int tick = 100;
 Object obj = new Object();
 public void run(){
  while(true){
   //此处有异常,只能try不能抛,因为子类覆写父类方法后只能声明抛出
   //父类异常的子集,如父类的这个方法没抛异常,子类只能try不能抛。
   synchronized(obj){
    if(tick>0){
    try{
     Thread.sleep(10);
    }catch(Exception e){}
     System.out.println(Thread.currentThread()+"\tsale : "+tick--);
    
    }
   }
  }
 }
}

class Thread02{
 public static void main(String args[]){
  Ticket t = new Ticket();//此方式不是在创建线程 ,而是建立要运行的代码
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  Thread t3 = new Thread(t);
  Thread t4 = new Thread(t);
  t1.start();
  t2.start();
  t3.start();
  t4.start();
  
 }
}

*/

//需求:银行有一个金库,有两个储户分别存300元,每次存100,存3次。
//该程序是否有安全问题,如果有,如何解决。
//如何找问题:
//1 ,明确哪些代码是多线程运行的共享代码,不是的,不需要同步!!
//2 ,明确共享数据。
//3 ,明确多线程运行代码中哪些语句是操作共享的。

/*
class Bank{
 private int sum; //共享数据
 Object obj = new Object();
 //此处可抛出异常,但在执行b.add(100);时,必须try不能再抛了,
 //因为子类Run中不能抛出父类没有的异常。
 public void add(int n){
  synchronized(obj){
   sum = sum +n;
   try{Thread.sleep(10);}catch(Exception e){}
   System.out.println("sum = "+sum);
  }
 }
}
class Cus implements Runnable{
 private Bank b = new Bank(); //共享数据
 public void run(){
  for(int x = 0;x<3;x++){
   b.add(100);  //
  }
 }
}
class Thread02{
 public static void main(String args[]){
  Cus c = new Cus();
  Thread t1 = new Thread(c);
  Thread t2 = new Thread(c);
  t1.start();
  t2.start();
 }
}
*/


/*
//如果你创建了两个线程,但运行各自的Run方法,那么也没必要用同步代码块!!
//因为各自Run方法中只有一个线程不存在内部竞争(没有安全问题)
class Bank{
 private int sum; //共享数据
 Object obj = new Object();
 public void add(int n){
  
   sum = sum +n;
   try{Thread.sleep(10);}catch(Exception e){}
   System.out.println(Thread.currentThread().getName()+"存放:"+n+"\t总钱数:"+sum);
  
 }
}
class Cus1 implements Runnable{
 private Bank b = new Bank(); //共享数据
 public void run(){
  for(int x = 0;x<3;x++){
   b.add(100);  //
  }
 }
}
class Cus2 implements Runnable{
 private Bank b = new Bank(); //共享数据
 public void run(){
  for(int x = 0;x<3;x++){
   b.add(1000);  //
  }
 }
}
class Thread02{
 public static void main(String args[]){
  Cus1 c1 = new Cus1();
  Cus2 c2 = new Cus2();
  Thread t1 = new Thread(c1,"A 用户:");//一个储户,存300
  Thread t2 = new Thread(c2,"B 用户:");//另一个储户,存300
  t1.start();
  t2.start();
  
 }
}
*/


/*
//这个里面必须要用同步锁 !!!!!!!!!
//创建储户的数量由线程的个数来决定
//储存的存数动作由各自用户中的Run方法来完成。
//如果你创建了两个线程,但运行各自的Run方法,那么也没必要用同步代码块!!
//因为各自Run方法中只有一个线程不存在内部竞争(没有安全问题)
class Bank{
 private int sum; //共享数据
 Object obj = new Object();
 public void add(int n){
  synchronized(obj){
   sum = sum +n;
   try{Thread.sleep(10);}catch(Exception e){}
   System.out.println(Thread.currentThread().getName()+"存放:"+n+"\t"+Thread.currentThread().getName().substring(0,1)+"家族总钱数:"+sum);
  }
 }
}
class Cus1 implements Runnable{
 private Bank b = new Bank(); //共享数据
 public void run(){
  for(int x = 0;x<3;x++){
   b.add(100);  //
  }
 }
}
class Cus2 implements Runnable{
 private Bank b = new Bank(); //共享数据
 public void run(){
  for(int x = 0;x<3;x++){
   b.add(1000);  //
  }
 }
}
class Thread02{
 public static void main(String args[]){
  Cus1 c1 = new Cus1();
  Cus2 c2 = new Cus2();
  Thread t1 = new Thread(c1,"A 用户:");//一个储户,存300
  Thread t3 = new Thread(c1,"A A 用户:");//一个储户,存300
  Thread t2 = new Thread(c2,"B 用户:");//另一个储户,存300
  Thread t4 = new Thread(c2,"B B 用户:");//另一个储户,存300
  t1.start();
  t3.start();
  t2.start();
  t4.start();
 }
}
*/
 

 

/*
    //同步函数代码块的使用           同步代码块  和同步函数。
class Bank{
 private int sum; //共享数据
 Object obj = new Object();
 public synchronized void add(int n){
  
   sum = sum +n;
   try{Thread.sleep(10);}catch(Exception e){}
   System.out.println(Thread.currentThread().getName()+"存放:"+n+"\t"+Thread.currentThread().getName().substring(0,1)+"家族总钱数:"+sum);
  
 }
}
class Cus1 implements Runnable{
 private Bank b = new Bank(); //共享数据
 public void run(){
  for(int x = 0;x<3;x++){
   b.add(100);  //
  }
 }
}
class Cus2 implements Runnable{
 private Bank b = new Bank(); //共享数据
 public void run(){
  for(int x = 0;x<3;x++){
   b.add(1000);  //
  }
 }
}
class Thread02{
 public static void main(String args[]){
  Cus1 c1 = new Cus1();
  Cus2 c2 = new Cus2();
  Thread t1 = new Thread(c1,"A 用户:");//一个储户,存300
  Thread t3 = new Thread(c1,"A A 用户:");//一个储户,存300
  Thread t2 = new Thread(c2,"B 用户:");//另一个储户,存300
  Thread t4 = new Thread(c2,"B B 用户:");//另一个储户,存300
  t1.start();
  t3.start();
  t2.start();
  t4.start();
 }
}

*/

/*
//需求:简单的卖票程序。多个窗口同时买票。
class Ticket implements Runnable{
 //静态的用法:生命调用长。
 private  int tick = 1000;
 Object obj = new Object();
 public synchronized void run(){  //如果在多线程中在Run方法上声明该字,那么程序相当于单线程,因为其它运行该方法
  while(true){     //的线程进不来!!!
   
    if(tick>0){
    try{
     Thread.sleep(10);
    }catch(Exception e){}
     System.out.println(Thread.currentThread()+"\tsale : "+tick--);
    
    }
   
  }
 }
}

class Thread02{
 public static void main(String args[]){
  Ticket t = new Ticket();//此方式不是在创建线程 ,而是建立要运行的代码
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  Thread t3 = new Thread(t);
  Thread t4 = new Thread(t);
  t1.start();
  t2.start();
  t3.start();
  t4.start();
  
 }
}

*/

 

/*
//此程序有三个线程,主线程启动后,创建对象,创建两个线程,开启了一个,这个线程不一定立刻执行,
//可能会执行t.flag = flase;所以打印结果不确定。//本程序有问题,因为不是用的同一个锁!!!!!!!!!!
class Ticket implements Runnable{
 //静态的用法:生命调用长。
 private  int tick = 100;
 public boolean flag = true;
 Object obj = new Object();
 public void run(){
  if(flag){
   while(true){
    synchronized(obj){
     if(tick>0){
      try{Thread.sleep(10);}catch(Exception e){}
      System.out.println(Thread.currentThread().getName()+".Code----"+tick--);
     }
    }
   }
  }else
   while(true)
    show();
 }
 public synchronized void show(){
  if(tick>0){
    try{
     Thread.sleep(10);
    }catch(Exception e){}
     System.out.println(Thread.currentThread().getName()+"**show ******"+tick--);
  }
 }
}

class Thread02{
 public static void main(String args[]){
  Ticket t = new Ticket();//此方式不是在创建线程 ,而是建立要运行的代码
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  t1.start();
  try{Thread.sleep(10);}catch(Exception e){}//主线程睡眠,让开启的线程运行,,此时只用一个线程可运行!!!
  t.flag = false;
  t2.start();
  
  
 }
}

*/

 

/*
//  由此证明是同一个this.
class Ticket implements Runnable{
 //静态的用法:生命调用长。
 private  int tick = 100;
 public boolean flag = true;
 //Object obj = new Object();
 public void run(){
  if(flag){
   while(true){
    synchronized(this){
     if(tick>0){
      try{Thread.sleep(10);}catch(Exception e){}
      System.out.println(Thread.currentThread().getName()+"..Code----"+tick--);
     }
    }
   }
  }else
   while(true)
    show();
 }
 public synchronized void show(){
  if(tick>0){
    try{
     Thread.sleep(10);
    }catch(Exception e){}
     System.out.println(Thread.currentThread().getName()+"*******show ******"+tick--);
  }
 }
}

class Thread02{
 public static void main(String args[]){
  Ticket t = new Ticket();//此方式不是在创建线程 ,而是建立要运行的代码
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  t1.start();
  try{Thread.sleep(8);}catch(Exception e){}//主线程睡眠,让开启的线程运行,,此时只用一个线程可运行!!!
  t.flag = false;
  t2.start();
  
  
 }
}
*/


/*
//静态同步方法,使用的锁是该方法所在类的字节码文件对象。类名。Class.
class Ticket implements Runnable{
 //静态的用法:生命调用长。
 static private int tick = 100;
 public boolean flag = true;
 //Object obj = new Object();
 public void run(){
  if(flag){
   while(true){
    synchronized(Ticket.class){
     if(tick>0){
      try{Thread.sleep(10);}catch(Exception e){}
      System.out.println(Thread.currentThread().getName()+"..Code----"+tick--);
     }
    }
   }
  }else
   while(true)
    show();
 }
 public static synchronized void show(){
  if(tick>0){
    try{
     Thread.sleep(10);
    }catch(Exception e){}
     System.out.println(Thread.currentThread().getName()+"*******show ******"+tick--);
  }
 }
}

class Thread02{
 public static void main(String args[]){
  Ticket t = new Ticket();//此方式不是在创建线程 ,而是建立要运行的代码
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  t1.start();
  try{Thread.sleep(8);}catch(Exception e){}//主线程睡眠,让开启的线程运行,,此时只用一个线程可运行!!!
  t.flag = false;
  t2.start();
  
  
 }
}
*/

 


/*
//饿汉式
class Single{
 private static final Single s = new Single();
 private Single(){}
 public static Single getInstance(){
  return s;
 }
}

//懒汉式加同步,低效!!
class Single{
 private static Single s = null;
 private Single(){}
 public static synchronized Single getInstance(){
  if(s==null)
   //-->A
   //-->B
   s = new Single();//对象被延迟。
  return s;
 }
}


//写一个延迟加载的单例设计模式    试例
//双重判断提高效率。实例的延迟加载,在多线程访问时,会出现安全问题,
//解决方案,有两种加同步锁函数或同步代码块都行,但稍有低效,但用双重
//判断的方式能解决这个效率问题,加同步时,如使用同步函数使用的锁是本类
//所属的字节码文件对象,它在类的加载时,产生,只加载一次。
class Single{
 private static Single s = null;
 private Single(){}
 public static  Single getInstance(){
  if(s==null){//
   synchronized(Single.class){//只要有一个对象初始化完,其它的都执行不到这里。减少了锁的判断。
    if(s==null)
     //-->A
     //-->B
     s = new Single();//对象被延迟。
   }
  }
   return s;
 }
}

*/  //重点!!!!!!!!!

 

  //死锁!!!!!!!!
/*
class Ticket implements Runnable{
 //静态的用法:生命调用长。
 private  int tick = 200;
 public boolean flag = true;
 Object obj = new Object();
 public void run(){
  if(flag){
   while(true){
    synchronized(obj){
     show();
    }
   }
  }else
   while(true)
    show();
 }
 public synchronized void show(){
  synchronized(obj){
     if(tick>0){
      try{Thread.sleep(10);}catch(Exception e){}
      System.out.println(Thread.currentThread().getName()+"..Code----"+tick--);
     }
    }
 }
}

class Thread02{
 public static void main(String args[]){
  Ticket t = new Ticket();//此方式不是在创建线程 ,而是建立要运行的代码
  Thread t1 = new Thread(t);
  Thread t2 = new Thread(t);
  t1.start();
  try{Thread.sleep(12);}catch(Exception e){}//主线程睡眠,让开启的线程运行,,此时只用一个线程可运行!!!
  t.flag = false;
  t2.start();
  
  
 }
}

*/


//死锁程序重点掌握!!!只有理解了死锁,写程序时,才会刻意避免它。
class Test implements Runnable{
 private boolean flag;
 Test(boolean flag){
  this.flag = flag;
 }
 public void run(){
  if(flag){
   while(true){
    synchronized(MyLock.locka){
     System.out.println("if locka");
     synchronized(MyLock.lockb){
      System.out.println("if lockb");
     }
    }
   }
  }else{
   while(true){
    synchronized(MyLock.lockb){
     System.out.println("else lockb");
     synchronized(MyLock.locka){
      System.out.println("else locka");
     }
    }
   }
  }
 }
}
class MyLock{
 static Object locka = new Object();
 static Object lockb = new Object();
}
class Thread02{
 public static void main(String args[]){
  Thread t1 = new Thread(new Test(true));
  Thread t2 = new Thread(new Test(false));
  t1.start();
  t2 .start();
 }
}

 

 

你可能感兴趣的:(黑马程序员--学习日记7(多线程 2 ) .)