java毕向东听课笔记15(线程4)

本文介绍了Java 1.5中多线程的升级方案,包括使用Lock替代Synchronized,以及利用Condition替换Object的wait/notify方法。同时,文章探讨了如何安全地停止线程,强调了正确控制循环来结束线程的重要性,并指出在线程冻结状态下,需要通过interrupt方法唤醒线程,而非直接终止。
摘要由CSDN通过智能技术生成

JDK1.5中提供了多线程升级解决方案。

将同步Synchronized替换为显示的Lock操作;将Object中的wait,notify,notifyAll替换为Condition对象,该对象可以通过Lock锁进行获取。


实例代码:


import java.util.concurrent.locks.*;

public class ProducerConsumerDemo{
	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;
	
	private Lock lock = new ReentrantLock();	//新建锁
	
	private Condition condition_pro = lock.newCondition();	//condition绑定Lock
	private Condition condition_con = lock.newCondition();	//1个lock可以绑定多个condition
	
	
	
	public void set(String name)throws InterruptedException{
		lock.lock();  //Lock的lock方法,加锁,替换synchronized
		try{
			while(flag)
				condition_pro.await();
			this.name = name +"--"+count++;
			
			System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
			flag = true;
			condition_con.signal();
		}
		finally{
			lock.unlock();	//Lock的unlock方法,释放锁
		}
	}
	
	public void out()throws InterruptedException{
		lock.lock();
		try{
			while(!flag)
				condition_con.await();
			System.out.println(Thread.currentThread().getName()+"...消费者......."+this.name);
			flag = false;
			condition_pro.signal();
		}
		finally{
			lock.unlock();
		}
	}
}

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

class Consumer implements Runnable{
	private Resource res;
	Consumer(Resource res){
		this.res = res;
	}
	public void run(){
		while(true){
			try{
				res.out();
			}
			catch(InterruptedException e){
				
			}
		}
	}
}
----------------------------------------------------------------------------------------------------------------

停止线程:

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(Thread.currentThread().getName()+"....Exception");
			}
			System.out.println(Thread.currentThread().getName()+"....run");
		}
	}
	public void changeFlag(){
		flag = false;
	}
}

public class StopThreadDemo{
	public static void main(String[] args){
		StopThread st = new StopThread();
		
		Thread t1 = new Thread(st);
		Thread t2 = new Thread(st);
		
		t1.start();
		t2.start();
		
		int num = 0;
		
		while(true){
			if(num++==60){
				st.changeFlag();
				break;
			}
			System.out.println(Thread.currentThread().getName()+"........"+num);
		}
	}
}

结果为:



程序没有结束,必须按ctrl+c强制结束。

interrupt方法是强制清除线程的冻结状态,恢复到运行状态,而不是结束线程。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值