day12综合练习

/*
1,等待唤醒机制 交替输入两个人的姓名和性别,交替输出
class Res
{
 private String name;
 private String sex;
 private boolean flag;
 public synchronized void set(String name,String sex)
 {
  if (flag)
  {
   try
   {
    this.wait();    
   }
   catch (Exception e)
   {
   }
  }
  this.name = name;
  this.sex = sex;
  flag = !flag;
  this.notify();
 }
 public synchronized void get()
 {
  if (!flag)
  {
   try
   {
    wait();    
   }
   catch (Exception e)
   {
   }
  }
  System.out.println(Thread.currentThread().getName()+name+sex);
  flag = !flag;
  notify();
 }
}
class In implements Runnable
{
 private Res r;
 In(Res r)
 {
  this.r = r;
 }
 public void run()
 {
  int flag = 0;
  while (true)
  {
   if (flag == 0)
   {
    r.set("张三","--男");
   }
   else
    r.set("李四","女");
   flag = ++flag%2;
  }
 }
}
class Out implements Runnable
{
 private Res r;
 Out(Res r)
 {
  this.r = r;
 }
 public void run()
 {
  while (true)
  {
   r.get();
  }
 }
}
class day12
{
 public static void main(String[] args)
 {
  Res r = new Res();
  new Thread(new In(r)).start();
  new Thread(new In(r)).start();
  new Thread(new Out(r)).start();
  new Thread(new Out(r)).start();
 }
}

7,使三个循环同时运行
class day12
{
 public static void main(String[] args)
 {
  new Thread()
  {
   public void run()
   {
    for (int x = 0;x < 100 ;x++ )
    {
     System.out.print(Thread.currentThread().getName()+x);
    }
   }
  }.start();
  Runnable r = new Runnable()
  {
   public void run()
   {
    for (int x = 0;x < 100 ;x++ )
    {
     System.out.print(Thread.currentThread().getName()+x);
    }
   }
  };
  new Thread(r).start();
  for (int x  = 0;x< 100; x++)
  {
   System.out.print(Thread.currentThread().getName()+x);
  }

 }
}
3,停止线程
4,守护线程
5,线程join方法
6,线程yield方法
class MThread implements Runnable
{
 private boolean flag = true;
 private int x = 0;
 public void changeFlag()
 {
  flag = false;
 }
 public synchronized void run()
 {
  while (flag)
  {
   System.out.println(Thread.currentThread().getName()+(++x));
   Thread.yield();
  }
 }
}
class day12
{
 public static void main(String[] args) throws Exception
 {
//  new Thread(new MThread()).start(); 
  Thread t = new Thread(new MThread());
  t.setDaemon(true);//守护线程,随主线程停止
  MThread stop = new MThread();
  new Thread(stop).start();
  for (int x = 0;x < 100 ;x ++ )
  {
   if (x == 50)
   {
    stop.changeFlag();
   }
   System.out.print(x);
  }
//  Thread t2 = new Thread(new MThread());
//  t2.join();
//  t2.start();
 }
}
2,生产者消费者,两种锁

*/


import java.util.concurrent.locks.*;
class Goods
{
 private String name;
 private int count;
 private boolean flag;
 private int x;
 private Lock lock = new ReentrantLock();
 private Condition pro = lock.newCondition();
 private Condition con = lock.newCondition();
 public void in(String name)
 {
  lock.lock();
  try
  {
   while (flag)
   {
    pro.await();
   }
   this.name = name;
   System.out.println(Thread.currentThread()+name+(++count));
   flag = true;
   con.signal();
  }
  catch (InterruptedException ie)
  {
  }
  finally
  {
   lock.unlock();
  }
 }
 public void out()
 {
  lock.lock();
  try
  {
   while (!flag)
   {
    con.await();
   }
   System.out.println(Thread.currentThread()+name+"消费");
   flag = false;
   pro.signal();
  }
  catch (InterruptedException ie)
  {
  }
  finally
  {
   lock.unlock();
  }
 }
}
class Pro implements Runnable
{
 private Goods g;
 Pro (Goods g)
 {
  this.g = g;
 }
 public void run()
 {
  while (true)
  {
   g.in("汽车");
  }
 }
}
class Cons implements Runnable
{
 private Goods g;
 Cons(Goods g)
 {
  this.g = g;
 }
 public void run()
 {
  while (true)
  {
   g.out();
  }
 }
}
class day12
{
 public static void main(String[] args)
 {
  Goods g = new Goods();
  new Thread(new Pro(g)).start();
  new Thread(new Pro(g)).start();
  new Thread(new Cons(g)).start();
  new Thread(new Cons(g)).start();
 }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值