黑马程序员技术交流社区
标题: 毕向东老师视频知识点笔记---多线程(2) [打印本页]
作者: 心在左边跳 时间: 2015-7-6 16:29
标题: 毕向东老师视频知识点笔记---多线程(2)
生产者和消费者- public class Res {
- private String name;
- private int count=1;
- private boolean flag=false;
- public synchronized void set(String name){
- if(flag)
- try{this.wait();}catch(Exception e){e.printStackTrace();}
- this.name=name+"---"+count++;
- System.out.println(Thread.currentThread().getName()+"--生产者--"+this.name);
- flag=true;
- this.notify();
- }
- public synchronized void out(){
- if(!flag)
- try{this.wait();}catch(Exception e){e.printStackTrace();}
- System.out.println(Thread.currentThread().getName()+"--消费者------"+this.name);
- flag=false;
- this.notify();
- }
- public static void main(String []args){
- Res r=new Res();
- 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 Producer implements Runnable{
- private Res res;
- Producer(Res res){
- this.res=res;
- }
- @Override
- public void run() {
- while(true){
- res.set("+商品+");
- }
- }
- }
- class Consumer implements Runnable{
- private Res res;
- Consumer(Res res){
- this.res=res;
- }
- @Override
- public void run() {
- while(true){
- res.out();
- }
- }
- }
复制代码 多个生产者和消费者- 对于多个生存者消费者,为什么要定义while标志?
唤醒线程后再判定一次flag - 为什么定义notifyAll?
唤醒对方线程,因为只用notify,容易出现只唤醒本方线程情况,导致程序所有线程等待。
- public class Res {
- private String name;
- private int count=1;
- private boolean flag=false;
- public synchronized void set(String name){
- while(flag){
- try{this.wait();}catch(Exception e){e.printStackTrace();}
- }
- this.name=name+"---"+count++;
- System.out.println(Thread.currentThread().getName()+"--生产者--"+this.name);
- flag=true;
- this.notifyAll();
- }
- public synchronized void out(){
- while(!flag){
- try{this.wait();}catch(Exception e){e.printStackTrace();}
- }
- System.out.println(Thread.currentThread().getName()+"--消费者------"+this.name);
- flag=false;
- this.notifyAll();
- }
- public static void main(String []args){
- Res r=new Res();
- 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 Producer implements Runnable{
- private Res res;
- Producer(Res res){
- this.res=res;
- }
- @Override
- public void run() {
- while(true){
- res.set("+商品+");
- }
- }
- }
- class Consumer implements Runnable{
- private Res res;
- Consumer(Res res){
- this.res=res;
- }
- @Override
- public void run() {
- while(true){
- res.out();
- }
- }
- }
复制代码java.util.concurrent.locks
只唤醒对方。
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- public class Res {
- private String name;
- private int count=1;
- private boolean flag=false;
- private Lock lock=new ReentrantLock();
- private Condition condition= lock.newCondition();
- public void set(String name) throws InterruptedException{
- lock.lock();
- try{
- while(flag){
- condition.await();
- }
- this.name=name+"---"+count++;
- System.out.println(Thread.currentThread().getName()+"--生产者--"+this.name);
- flag=true;
- condition.signalAll();
- }finally{
- lock.unlock();}
- }
- public void out()throws InterruptedException{
- lock.lock();
- try{
- while(!flag){
- condition.await();
- }
- System.out.println(Thread.currentThread().getName()+"--消费者------"+this.name);
- flag=false;
- condition.signalAll();
- }finally{
- lock.unlock();}
- }
- public static void main(String []args){
- Res r=new Res();
- 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 Producer implements Runnable{
- private Res res;
- Producer(Res res){
- this.res=res;
- }
- @Override
- public void run() {
- while(true){
- try {
- res.set("+商品+");
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- }
- class Consumer implements Runnable{
- private Res res;
- Consumer(Res res){
- this.res=res;
- }
- @Override
- public void run() {
- while(true){
- try {
- res.out();
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- }
复制代码 停止线程开启多线程运行,通常代码是循环结构,只要控制循环,就可以让RUN方法结束。结束线程。特殊情况,当线程处于冻结状态,就不会读取到标记,线程不会结束。
interrupt()方法强制清除冻结状态。这样可以操作标记让线程结束。
- public class StopThread implements Runnable {
- private boolean flag=true;
- @Override
- public synchronized void run() {
- while(flag){
- try{
- wait();
- }catch(InterruptedException e){
- e.printStackTrace();
- flag=false;
- }
- }
- System.out.println(Thread.currentThread().getName()+"----run");
- }
- public void changeFlag(){
- flag=false;
- }
- public static void main(String[]args){
- Thread t=new Thread(new StopThread());
- t.start();
- t.interrupt();
- }
- }
复制代码 守护线程setDeamon(true)//标记线程为守护线程(后台线程)。主线程是前台线程,当所有前台线程结束后,后台线程自动结束。
- public static void main(String[]args){
- Thread t=new Thread(new StopThread());
- t.setDeamon(true);
- t.start();
- t.interrupt();
复制代码 join()方法new Thread(r).join()//该线程抢夺执行权,让主线程冻结。
当A线程执行到B线程join()方法时,A就会等待,等B线程执行完,A线程才会执行。
join用来临时加入线程执行。
优先级和yield谁开启线程,线程就属于哪个组。例如main线程组
优先级1-10,通常是5
t.setPriority(Thread.MAX_PRIORITY)
yield,暂停当前线程对象,执行其他线程。
Thread.yield();//临时施放线程资源。平均运行效果。
毕向东老师视频知识点笔记---多线程(1)
作者: 王文辉 时间: 2015-7-6 17:15
支持,用心了,加油
作者: xiaoxinxin003 时间: 2015-7-6 18:30
毕老师的视频真不错
作者: 路途 时间: 2015-7-6 18:39
最好代码上加注释啊!!这样过一段时或者别人看起来不直观啊。。。。。。。。。。
作者: 心在左边跳 时间: 2015-7-6 19:40
都是小例子,逻辑很简单。这些例子都是对代码前面知识点说明。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |