现在的位置: 首页 > 综合 > 正文

java 实现互斥与共享

2014年02月15日 ⁄ 综合 ⁄ 共 1863字 ⁄ 字号 评论关闭

public class TestThread {

 

// 共享资源

private String[] lockObj = { "true" };

   public static void main(String[] args) {

      TestThread myThread = new TestThread();

        // 定义线程

        NotifyThread notifyThread = myThread.new NotifyThread("notifyThread");

        WatiThread wait01 = myThread.new WatiThread("wait01");
                //设置线程权限
        wait01.setPriority(2);

        WatiThread wait02 = myThread.new WatiThread("wait02");

        wait02.setPriority(3);

        WatiThread wait03 = myThread.new WatiThread("wait03");

        wait03.setPriority(4);

        

        // 启动线程

        notifyThread.start();

        wait01.start();

        wait02.start();

        wait03.start();
 

}

 

// 定义通知线程

class NotifyThread extends Thread {

    public NotifyThread(String _sThreadName) {

        super(_sThreadName);

    }

 

    public void run() {
    // 休眠2秒,给等待线程等待时间
        try {
               sleep(2000);

        } catch (InterruptedException e) {
               e.printStackTrace();
            }

    

        synchronized (lockObj) {

            System.out.println("线程[" + getName() + "]开始准备通知.....");

            lockObj[0] = "false";

            lockObj.notifyAll();  //该成lockObj.notify();则运行测试的时候进程没有结束,因为我们还有2个线程没有结束

            System.out.println("线程[" + getName() + "]开始通知结束.....");

        }

        System.out.println("线程[" + getName() + "]运行结束.....");

    }

    

}

 

// 定义等待的线程类

class WatiThread extends Thread {

        public WatiThread(String _sThreadName) {

           super(_sThreadName);

                }

 

    public void run() {

 

    synchronized (lockObj) {

       while (lockObj[0].equalsIgnoreCase("true")) {

            System.out.println("线程[" + this.getName() + "]开始等待....");
            //获得系统的当前时间
            long lWaitTime = System.currentTimeMillis();
            try {

               lockObj.wait();

            } catch (InterruptedException e) {

               e.printStackTrace();

            }

            lWaitTime = System.currentTimeMillis() - lWaitTime;

            System.out.println("线程[" + getName() + "]等待时间为:"+ lWaitTime);

         }

    }

    

    System.out.println("线程[" + getName() + "]等待结束!");

   }

   }

}

抱歉!评论已关闭.