java 多线程

线程间通信

思考1:wait(),notify(),notify All(),用来操作线程为什么定义在了Object类中?

1,这些方法存在与同步中。
2,使用这些方法时必须要标识所属的同步的锁。
3,锁可以是任意对象,所以任意对象调用的方法一定定义Object类中。
思考2:wait(), sleep()有什么区别?
wait():释放资源,释放锁。sleep():释放资源,不释放骑士

class Res
{
	string name;
	string sex;
}
class Input implements Runnble
{
	private Res r;
	Input(Res r)
	{
		this.r = r;
	}
	public void run()
	{
		int x = 0;
		 while (true)
		{
			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)
		{
			System.out.println(r.name + "...." + r.sex);
		}
	}
}
class  InputOutputDemo
{
	public static void main(String[] args)
	{
		Res r = now Res();

		Input in = new Input(r);
		Output out = new Output(r);

		Thread t1 = new Thread(in);
		Thread t2 = new Thread(out);

		t1.start();
		t2.start();
	}

}

import java.util.concurrent.locks.*;
class  ProducerConsumerDemo2
{
	public static void main(String[] arqs)
	{
		Resource r = new Resource();

		Producer pro = new Producer(r);
		Consumer con = new Consumer(r);

		Thread t1≡new = new Thread(pro);
		Threadt2 = new Thread(pro);
		Threadt3≡new = new Thread(con);
		Threadt4 = new Thread(con);

		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
}
	
		/*
		JDK1.5中提供了多线程升级解决方案。
		将同步 synchronized 替换成现实Lock操作。将Object中的wait, notify notify All,替换了condition对象。
		该对象可以Lock锁进行获取。
		*/
class Resource
{
	private String name;
	private int count = 1;
	private boolean flag = false;
//t1 t2
	private Lock lock = new  HeentrantLock ();
	private Condition condition _ con = lock.newCondition();

	public void set(String name) throws  InterruptedException
	{
		lock.lock();
		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();
		}
	}
//tɔt4
	public void out()throws InterruptedException 
	{
		lock.lock();
		try
		{
			while (!flag)
				condition_con.await();
			system.out.printin(Thread.currentThread().getName() + "...消费者..." + this.name);
			flag = false;
			condition_pro.srgnal();
		}
		finally
		{
			lock.unlock();
		}
	}

停止线程

1.定义循环结束标记
·因为线程运行代码一般都是循环,只要控制了循环即可。
2.使用interrupt(中断)方法。
该方法是结束线程的冻结状态,使线程回到运行状态中来。
注:stop方法已经过时不再使用。 

top方法已经过时,
如何停止线程?
有一种,run方法结束。
开启多线程运行,运行代码通常是循环结构。
只要控制住循环,就可以让run方法结束,也就是线程结束。
特殊情况
当线程处于了冻结状态。
就不会读取到标记γ那么线程就不会结束。
当没有指定的方式让冻结的线程恢复到运行状态是,这时需要对冻结进行清除。强制让线程恢复到运行状态中来。这样就可以操作标记让线程结束。
Thread类提供该方法interrupt();

class  StopThread   implements  Runnable
	{
		private boolean flag = true;
		public synchronized void run()
		{
			while (flag)
			{
				try
				{
					wait();
				}
				catch (InterruptedException e)
				{
					System.out.printin(Thread.currentThread().getName() + ".... Excaption");
				}
				System.out.println(Thread.currentThread().getName() + ".......");

			}
		}
		public void  changeFlag()
		{
			flag = false;
		}
	}

	class  StopThreadDemo
	{
		public static void main(String[] args)
		{
			StopThread st = new  StopThread ();

			Threadt1 = new Thread(st);
			Threadt2 = 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):

			}
			system.out.println("over");
		}
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值