----------------------
ASP.Net+Android+IOS开发、
.Net培训、期待与您交流! ----------------------
4、多线程在开发中常见应用
---------------------- ASP.Net+Android+IOS开发、 .Net培训、期待与您交流! ----------------------
1、等待唤醒机制
可能有些难理解,多敲几遍,多看注释
关于用到的一些概念
wait:
notify();
notifyAll();
它门都使用在同步中,因为要对持有监视器(锁)的线程操作。所以要使用在同步中,因为只有同步才具有锁。
为什么这些操作线程的方法要定义Object类中呢?
因为这些方法在操作同步中线程时,都必须要标识它们所操作线程只有的锁,只有同一个锁上的被等待线程,可以被同一个锁上notify唤醒。不可以对不同锁中的线程进行唤醒。也就是说,等待和唤醒必须是同一个锁。而锁可以是任意对象,所以可以被任意对象调用的方法定义Object类中。
class Res//这是线程共同操作的资源类
{
private String name;
private String sex;
private boolean flag = false;//用来控制线程的等待唤醒状态
//设置姓名性别的同步函数
public synchronized void set(String name,String sex)
{
//若flag为真此时线程等待
if(flag)
try{this.wait();}catch(Exception e){}//这个异常只能try
this.name = name;
this.sex = sex;
flag = true;
this.notify();//用锁去调用
}
public synchronized void out()//打印方法
{
if(!flag)
try{this.wait();}catch(Exception e){}
System.out.println(name+"........"+sex);
flag = false;//打印完再置为假
this.notify();
}
}
class Input implements Runnable
{
private Res r ;
Input(Res r)
{
this.r = r;
}
public void run()
{
int x = 0;//是为了交替赋值
while(true)
{
if(x==0)
r.set("mike","man");
else
r.set("丽丽","女女女女女");
x = (x+1)%2;//0给它返回1,1给它返回0
}
}
}
class Output implements Runnable
{
private Res r ; //确保资源对象的唯一
Output(Res r)//对象初始化是要有资源传入
{
this.r = r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
class InputOutputDemo2
{
public static void main(String[] args)
{
Res r = new Res();
//用匿名对象调用
new Thread(new Input(r)).start();
new Thread(new Output(r)).start();
}
}
2、生产者消费者
这个很重要!要掌握
class Resource//这就是一个资源的类
{
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name)
{
//这边只能用while保证线程万一在下一句失去执行权之后重新回来执行时能再判断
while(flag)
try{this.wait();}catch(Exception e){}
this.name = name+"--"+count++; //把名称和个数都给name
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;//将flag置为true然后继续执行到while时就会失去执行资格
this.notifyAll();//要唤醒所有线程,notify的话,有可能会唤醒本方的线程,那么程序就挂了
}
public synchronized void out()
{
while(!flag)//同理要是本线程先到这边因为set还没赋值那么就等着
try{wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
flag = false;
this.notifyAll();
}
}
class Producer implements Runnable //这是生产者,这边才是能够进行多线程操作的类
{
private Resource res;
Producer(Resource res) //为了保证资源对象的统一,我们设计从外部传
{
this.res = res;
}
public void run()
{
while(true)
{
res.set("+商品+");
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
res.out();
}
}
}
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();
}
}
3、JDK1.5中提供了多线程升级解决方案。新的生产者消费者代码
将同步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();//Lock是接口,应该是ReentrantLock中重写了方法
//注意一个lock锁上可以有多个相关的condition
private Condition condition_pro = lock.newCondition();//Condition也是接口
//lock.newCondition返回绑定到此 Lock 实例的新 Condition 实例。
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();//释放锁的动作一定要执行。
}
}
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();//唤醒set里面的线程
}
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,这里要catch下
}
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)
{
}
}
}
}
4、多线程在开发中常见应用
//在下面的代码中原本有三个for循环,如果我不线程封装起来的话,只能一个接一个的运行
class ThreadTest
{
public static void main(String[] args)
{
//建一个Thread的匿名子类
new Thread()
{
//要覆盖run方法
public void run()
{
for(int x=0; x<100; x++)
{
System.out.println(Thread.currentThread().getName()+"....."+x);
}
}
}.start();
for(int x=0; x<100; x++)
{
System.out.println(Thread.currentThread().getName()+"....."+x);
}
//建立一个Runnable接口的子类对象
Runnable r = new Runnable()
{
public void run()
{
for(int x=0; x<100; x++)
{
System.out.println(Thread.currentThread().getName()+"....."+x);
}
}
};
new Thread(r).start();
}
}
---------------------- ASP.Net+Android+IOS开发、 .Net培训、期待与您交流! ----------------------