黑马程序员_多线程间通讯

来源:互联网 发布:淘宝店差评可以改吗 编辑:程序博客网 时间:2024/05/16 11:40

线程间通讯

 

class Rec{String name;String sex;}class Input implements Runnable{private Rec r;Input(Rec r){this.r = r;}public void run(){int x = 0while(true){if(x = 0){r.name = "milk";r.sex = "man";}else{r.name = "露丝";r.sex = "女";}x = (x+1)%2;}}}class Output implements Runnable{private Rec;Output(Rec r){this.r = r;}public void run(){while(true){System.out.println(r.name+"…."+r.sex);}}}class Test{public static void main(String[] args){Rec r = new Rec();Input in = new Input(r);Output out = new Output(r);Thread t1 = new Thread(in);Thread t2 = new Thread(out);<pre class="java" name="code">t1.start();t2.start();}}

 


线程间实现了通讯,但是不安全

 

线程间通讯-解决安全问题

 

class Input implements Runnable{private Rec r;Input(Rec r){this.r = r;}public void run(){int x = 0while(true){synchronized(r){if(x = 0){r.name = "milk";r.sex = "man";}else{r.name = "露丝";r.sex = "女";}}x = (x+1)%2;}}}class Output implements Runnable{private Rec;Output(Rec r){this.r = r;}public void run(){while(true){synchronized(r)System.out.println(r.name+“….”+r.sex);}}}


多线程-等待唤醒机制


class Rec{       String name;       String sex;       boolean flag = false;} class Input implements Runnable{       private Rec r;       Input(Rec r){       this.r = r;}public void run(){       int x = 0       while(true){       synchronized(r){       if(r.flag)              try{r.wait();}catch(Exceptione){}              if(x = 0){              r.name = “milk”;              r.sex = “man”;}else{              r.name = "露丝";              r.sex = "女";}r.flag = true;       r.notify();                     }x = (x+1)%2;}}}<pre class="java" name="code">class Output implements Runnable{private Rec;Output(Rec r){this.r = r;}public void run(){while(true){synchronized(r)if(!r.flag)//因为是同一把锁,因此上面是false,wait,下面则要取反值try{r.wait();}catch(Exception e){}System.out.println(r.name+“….”+r.sex);r.flag = false;r.notify();}}}

 

代码优化

 

class Rec{private String name;private String sex;private 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;this.flag = true;this.notify();}public synchronized void out(){if(!flag)try{r.wait();}catch(Exception e){}System.out.println(this.name+"……."+this.sex);this.flag = false;this.notify();}}class Input implements Runnable{private Rec r;Input(Rec r){this.r = r;}public void run(){int x = 0while(true){if(x = 0)r.set("milk","man");elser.set("露丝","女");x = (x+1)%2;}}}class Output implements Runnable{private Rec;Output(Rec r){this.r = r;}public void run(){while(true){r.out();}}}


线程间通讯-生产者消费者

class Resource{private String name;private int count = 1;private boolean flag = false;public synchronized void set(String name){while(flag)try{wait();}catch(Exception e){}this.name = name+"--"+count++);System.out.println(Thread.currentThread().getName +" ……生产者……"+ths.name);this.flag = true;this.notifyAll();}pubic synchronized void out(){while(!flag)try{wait();}catch(Exception e){}System.out.println(Thread.currentThread().getName+"…...消费者………."+this.name);this.flag = false;this.notifyAll();}}class Producter implements Runnable{private Resource  r;producter(Resource r){this.r  = r;}public void run(){while(true){r.set(product);}}}class Customer implements Runnable{private Resource  r;Customer(Resource  r){this.r = r;}public void run(){while(true){r.out();}}}class Test{public static void main(String[] args){Resource  r = new Resource();Producter  p = new Producter(r);Customer  c = new Customer(r);Thread t1 = new Thread(p);Thread t2 = new Thread(p);Thread t3 = new Thread(c);Thread t4 = new Thread(c);t1.start();t2.start();t3.start();t4.start();}}

线程间通讯-生产者消费者JDK5.0升级版

 

将同步synchronized替换成现实Lock操作

将Object中的wait,notify,notifyAll,替换成Condition对象

该对象可以从Lock锁进行获取

该示例中,实现了本方只唤醒对方的操作

 

class Resource{private String name;private int count = 1;private boolean flag = false;private Lock lock = new ReentrantLock();private Condition condition_pro = lock.newCondition();private Condition condition_con = lock.newCondition();public void set(String name){try{lock.lock();while(flag)conditon_pro.await();this.name = name+"--"+count++);System.out.println(Thread.currentThread().getName+"…生产者……"+ths.name);this.flag = true;condition_con.signal();}finally{lock.unlock();}}pubic synchronized void out(){try{lock.lock();while(!flag)condition_con.await();System.out.println(Thread.currentThread().getName+"...消费者……"+this.name);this.flag = false;condition_pro.signal();}finally{lock.unLock();}}}class Producter implements Runnable{private Resource  r;producter(Resource r){this.r  = r;}public void run(){while(true){r.set(product);}}}class Customer implements Runnable{private Resource  r;Customer(Resource  r){this.r = r;}public void run(){while(true){r.out();}}}class Test{public static void main(String[] args){Resource  r = new Resource();Producter  p = new Producter(r);Customer  c = new Customer(r);Thread t1 = new Thread(p);Thread t2 = new Thread(p);Thread t3 = new Thread(c);Thread t4 = new Thread(c);t1.start();t2.start();t3.start();t4.start();}}


停止线程

 

stop方法已经过时

如何停止线程

只有一种,run方法结束

开启多线程运行,运行代码通常是循环结构

只要控制住循环,就可以让run方法结束,也就是线程结束

 

特殊情况

当线程处于冻结状态

就不会读取到标记,那么线程就不会结束

 

当没有指定的方式让冻结的线程恢复到运行状态时,这时需要对冻结状态进行清除

强制让线程恢复到运行状态中来,这样就可以操作标记让线程结束

 

class StopThread implements Runnable{private boolean flag = true;public synchronized void run(){while(flag){try{wait();}catch(InterruptedException e){System.out.println("………..Exception");flag = false;}System.out.println("……..StopThread run…….");}}public void checkFlag(){this.flag = false;}}class Test{public static void main(String[] args){StopThread st = new StopThread();Thread t = new Thread(st);t.start();int x = 0;while(true){if(x++ == 60){t.interrupt();//叫醒冻结状态的线程并使他结束break;}System.out.println(Thread.currentThread()+"……main run…….."+x);}}}


 

 

守护线程

将t标记为守护线程

 

class Test{public static void main(String[] args){StopThread st = new StopThread();Thread t = new Thread(st);t.setDaemon(true);//daemon:希腊神话中的守护神t.start();int x = 0;while(true){if(x++ == 60){t.interrupt();//叫醒冻结状态的线程并使他结束break;}System.out.println(Thread.currentThread()+"……main run…….."+x);}}}


 

 

join方法

join:

当A线程执行到B线程的b.join()方法时,A就会等待,等B线程都执行完,A才会执行

 

霸道强势获取CUP的执行权使原本的线程处于冻结状态,例如主线程在执行过程中遇到Thread对象的join方法就会停止,等Thread对象执行完了之后在继续执行主线程

 

优先级&yield方法


setPriority():

设置线程的优先级

格式:线程对象.setPriority(Thread.MAX_PRIORITY)


 yield():

减缓线程的运行频率,使线程都能执行到,平均开来

Thread.yield();



0 0
原创粉丝点击