黑马程序员---java学习笔记之多线程2

1、Object类中的wait方法,notify方法,notifyAll方法只能用在同步中(同步函数或者同步代码块)。

2、视频中老师线程通信的生产者与消费者之间的例子(我做了一些修改)

public class ProducerConsumerDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Resource res =  new Resource();
		new Thread(new Producer(res)).start();
		new Thread(new Consumer(res)).start();
		
	}

}
class Resource
{
	private String name;
	private int counter = 1;
	private boolean flag = false;
	
	public synchronized void produce(String name)
	{
		if(flag)
			try{wait();}catch(Exception e){}
		this.name = name+"---"+(counter++);
		
		System.out.println(Thread.currentThread().getName()+"+生产者+"+this.name);
		flag = true;
		notify();	
	}
	public synchronized void consume()
	{
		if(!flag)
			try{wait();}catch(Exception e){}
		System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
		flag = false;
		notify();
	}
	public int getCounter()
	{
		return counter;
	}
}
class Producer implements Runnable
{
	Resource res;
	public Producer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.produce("商品");
			if(res.getCounter() > 100)
				break;
		}
	}
}
class Consumer implements Runnable
{
	Resource res;
	public Consumer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.consume();
			if(res.getCounter() > 100)
				break;
		}
	}
}
该代码示例的结果是生产者与消费者交替输出。

运行结果如下:

Thread-0+生产者+商品---1
Thread-1---消费者---商品---1
Thread-0+生产者+商品---2
Thread-1---消费者---商品---2
Thread-0+生产者+商品---3
Thread-1---消费者---商品---3
Thread-0+生产者+商品---4
Thread-1---消费者---商品---4

。。。。。。

当有多个生产者或者消费者的时候,主函数代码需要做一些修改:

public static void main(String[] args) {
		// TODO Auto-generated method stub

		Resource res =  new Resource();
		
		Producer p1 = new Producer(res);
		Producer p2 = new Producer(res);
		
		
		Consumer c1 = new Consumer(res);
		Consumer c2 = new Consumer(res);
		
		Thread t1 = new Thread(p1);
		Thread t2 = new Thread(p2);
		Thread t3 = new Thread(c1);
		Thread t4 = new Thread(c2);
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		
		
	}
这样运行结果就出现了错误:

或者

生产的两件商品,只被消费了一次;或者一件商品被消费了两次。所以还是要修改同步代码块中的代码:

public class ProducerConsumerDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Resource res =  new Resource();
		
		Producer p1 = new Producer(res);
		Producer p2 = new Producer(res);
		
		
		Consumer c1 = new Consumer(res);
		Consumer c2 = new Consumer(res);
		
		Thread t1 = new Thread(p1);
		Thread t2 = new Thread(p2);
		Thread t3 = new Thread(c1);
		Thread t4 = new Thread(c2);
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();
		
		
	}

}
class Resource
{
	private String name;
	private int counter = 1;
	private boolean flag = false;
	
	public synchronized void produce(String name)
	{
		while(flag)
			try{wait();}catch(Exception e){}
		this.name = name+"---"+(counter++);
		
		System.out.println(Thread.currentThread().getName()+"+生产者+"+this.name);
		flag = true;
		notifyAll();	
	}
	public synchronized void consume()
	{
		while(!flag)
			try{wait();}catch(Exception e){}
		System.out.println(Thread.currentThread().getName()+"---消费者---"+this.name);
		flag = false;
		notifyAll();
	}
	public int getCounter()
	{
		return counter;
	}
}
class Producer implements Runnable
{
	Resource res;
	public Producer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.produce("商品");
			if(res.getCounter() > 100)
				break;
		}
	}
}
class Consumer implements Runnable
{
	Resource res;
	public Consumer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			res.consume();
			if(res.getCounter() > 100)
				break;
		}
	}
}

这样就可以运行正常了。

3、下面是对jdk升级5.0版之后,同步在生产者与消费者的例子中的应用

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ProducerConsumerDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Resource res =  new Resource();
		
		Producer p1 = new Producer(res);
		Producer p2 = new Producer(res);
		
		
		Consumer c1 = new Consumer(res);
		Consumer c2 = new Consumer(res);
		
		Thread t1 = new Thread(p1);
		Thread t2 = new Thread(p2);
		Thread t3 = new Thread(c1);
		Thread t4 = new Thread(c2);
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}

}
class Resource
{
	private String name;
	private int counter = 1;
	private boolean flag = false;
	private Lock lock = new ReentrantLock();
	private Condition condition_pro  = lock.newCondition();
	private Condition condition_con  = lock.newCondition();
	
	
	public  void produce(String name) throws InterruptedException
	{
		lock.lock();
		try
		{
			while(flag)
				condition_pro.await();
			this.name = name+"---"+(counter++);
		
			System.out.println(Thread.currentThread().getName()+"+生产者+"+this.name);
			flag = true;
			condition_con.signal();
		}
		finally
		{
			lock.unlock();	
		}
	}
	public  void consume() 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();
		}
	}
	public int getCounter()
	{
		return counter;
	}
}
class Producer implements Runnable
{
	Resource res;
	public Producer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			try
			{
			res.produce("商品");
			}
			catch(InterruptedException e)
			{
				
			}
			if(res.getCounter() > 100)
				break;
		}
	}
}
class Consumer implements Runnable
{
	Resource res;
	public Consumer(Resource res)
	{
		this.res = res;
	}
	public void run()
	{
		while(true)
		{
			try
			{
				res.consume();
			}
			catch(InterruptedException e)
			{
				
			}
			if(res.getCounter() > 100)
				break;
		}
	}
}
这代码相当地经典。

4、如何停止一个线程?

        因为Thread类中的stop方法已经过时,所以只有一种方法可以使线程结束,就是run方法执行结束。

5、怎么清除一个线程的冻结状态?

        Thread类中有一个interrupt的方法,可以清除某线程的冻结状态,使它强制转换到运行状态。

6、当某线程遇到wait语句时,会把该线程原本具有的锁(监视器)释放。

7、守护线程

        a):主线程是前台线程,被标记为守护线程的线程是后台线程,但主线程结束的时候,守护线程自动结束;

        b):把某线程标记为守护线程的方法,是Thread类中的setDeamon方法,另外记住,标记某线程为守护线程的代码要写在该线程启动(start方法)之前。

8、Thread类中的join方法

        当某一线程执行代码中有“另一线程.join();”这样的代码时,当前线程就会被冻结,释放自己的执行资格,直到另一线程执行完毕,原本的线程才会重新执行。

9、Thread类中有一个toString方法,重写了Object类中的toString方法,打印输出的是线程名,优先级和线程组。

        线程组:某个线程是由谁开启的,这个线程就属于谁的线程组;

        优先级:是cpu执行某个线程的频率,主线程包括其他所有线程的默认优先级都是5,优先级分为10级,1-10。Thread类中的3个静态字段表示的就是1,5,10的优先级值。l另外,设置优先级的方法,是Thread类中的setPriority(int x)方法。

10、多线程的yield方法,暂时停止某个线程。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值