A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© tangxiaobo1991 中级黑马   /  2014-3-10 10:10  /  1343 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本人最近学习多线程,感觉颇揪心。遂一鼓作气将学习中的实例总结出来与大家分享,主要是等待唤醒机制的例子。本人直接粘贴不用大家下载拉!见下面
一:/*

需求:逐个依次打印生产者和消费者,并用数字记录着结果。
方法:运用wait和notify方法实习逐次打印
步骤:设置生产者和消费者两种线程。每种线程有两个线程。
*/
//import  java.util.concurrent.locks.*;

/*class Res
{
       
        private int count=1;
        private String name;
        boolean flag=true;
        public synchronized void set()
        {  
        while(true)
          {
                while(!flag)
                  try{this.wait();}catch(Exception e){}
                      System.out.println(Thread.currentThread().getName()+"生产者"+count++);
                      flag=false;
                      this.notifyAll();
                }     
                  
                  
        }
        public synchronized void out()
        {
                while(true)
                {
                        while(flag)
                        try{this.wait();}catch(Exception e){}
                                System.out.println(Thread.currentThread().getName()+"消费者"+count);
                                flag=true;
                                this.notifyAll();
                }
        }
}

//_________________________________________________________

class Producer implements Runnable
{
        private Res r;
        Producer(Res r)
        {
         this.r=r;
        }
        public void run()
        {
                r.set();
        }
       
}


//____________________________________________________________

class Consumer implements Runnable
{
        private Res r;
        Consumer(Res r)
        {
                this.r=r;
        }
        public void run()
        {
          
             r.out();
             
        }
}






//____________________________________________________________

class ProducerConsumer
{
        public static void main(String[] args)
        {
                Res r=new Res();
                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();
               
        }
}













*/
//------------------------------------------------

二:
/*
需求:逐次答应生产者和消费者
要求:使用lock和condition的方法编写。
步骤:1,设置一个lock对象;
          2,建立两个condition对象。
          
*/

/*
import java.util.concurrent.locks.*;
class Res2
{
        private int count=1;
        private count++;
        private boolean flag=true;
        private Lock lock=new  ReentrantLock();
       
        private Condition condition_pro=lock.newCondition();
        private Condition condition_con=lock.newCondition();
         
        public void set() throws InterruptedException
        {
               
                lock.lock();
                try
                {
                while(!flag)
                condition_pro.await();
                System.out.println(Thread.currentThread().getName()+"----生产者----"+count);
                flag=false;
                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()+"消费者"+count);
                flag=true;
                condition_pro.signal();
                }
               
               
                finally
                {
                lock.unlock();
                }
        }
}

//----------------------------------------------------------------------------------------

class Producer2 implements Runnable
{
        private Res2 r;
        Producer2(Res2 r)
        {
        this.r=r;
        }
        public void run()
        {
        try
        {
        while(true)
        r.set();
        }
        catch(Exception e){}
        }
}

//----------------------------------------------------------------------------------------
class Consumer2 implements Runnable
{
        private Res2 r;
        Consumer2(Res2 r)
        {
        this.r=r;
        }
        public void run()
        {
        try
        {
        while(true)
        r.out();
        }
        catch(Exception e){}
        }
}

//-----------------------------------------------------------------------------------------


class ProduceConsumerDemo2
{
        public static void main(String[] args)
        {
        Res2 r=new Res2();
        Producer2 pro=new Producer2(r);
        Consumer2 con=new Consumer2(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();
        }
}





*/

//-----------------------------------------------------
三:
/*
需求:演示中断线程的程序;
技术: interrupte()函数的应用。
*/


class StopThread implements Runnable
{
   boolean flag=true;
   public synchronized void run()
   {
  while(flag)
   {
           try
           {
           wait();//wait后其他线程仍然有机会获得线程执行权。
           }
            catch(Exception e){ flag=false;}



     System.out.println(Thread.currentThread().getName()+"----run");
           
    }
    }

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


class StopThreadDemo
{
        public static void main(String[] args)
        {
                int number=0;
                StopThread st=new StopThread();
                Thread t1=new Thread(st);
                Thread t2=new Thread(st);
                //t1.setDaemon(true);主线程一旦结束,该线程也就自动结束
                //t2.setDaemon(true);同上
                t1.start();
                t2.start();
               
                while(true)
                {
                        if(number++==600)
                        {
                                //st.changeFlag();
                                //t1.interrupt();
                                //t2.interrupt();
                                //try{Thread.sleep(100);}catch(Exception e){}
                               
                                break;
                        }
                        System.out.println(Thread.currentThread().getName()+"---run-----"+number);
                }
        }
}

2 个回复

倒序浏览
加油吧 小伙伴
回复 使用道具 举报

加油吧 小伙伴
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马