zoukankan      html  css  js  c++  java
  • 在一个类中写完多线程(sleep 方法和wait 方法的区别)

    继承Thread()类完成多线程,一般需要4步:

    (1)新建一个MyThread类继承Thread类

    (2)在MyThread类中重写run()方法

    (3)在主线程中创建线程对象 Thread t = new Thread();

    (4)在主线程中启动线程t.start()

    上述过程,要分别在两个类中实现。

    下面的将展示在一个类中实现

     1 public class SleepWaitDemo {
     2     public static void main(String[] args) {
     3         final Object lock = new Object();
     4         // 创建A线程,并启动
     5         new Thread(new Runnable() {
     6             public void run() {
     7                 System.out.println("thread A is waiting to  get lock");
     8                 synchronized (lock) {
     9                     try {
    10                         System.out.println("thread A get lock");
    11                         Thread.sleep(20);//sleep()仅释放CPU但不释放锁
    12                         System.out.println("thread A  do wait method");
    13                         lock.wait(1000);//wait()释放CPU和锁
    14                         System.out.println("thread A is done");
    15                     } catch (InterruptedException e) {
    16                         e.printStackTrace();
    17                     }
    18                 }
    19             }
    20         }).start();// 创建A线程并启动完毕
    21         
    22         try {
    23             Thread.sleep(10);//主线程sleep 10ms
    24         } catch (InterruptedException e1) {
    25             e1.printStackTrace();
    26         }
    27         
    28         // 创建B线程,并启动
    29                 new Thread(new Runnable() {
    30                     public void run() {
    31                         System.out.println("thread B is waiting to get lock");
    32                         synchronized (lock) {
    33                             try {
    34                                 System.out.println("thread B  get lock");
    35                                 System.out.println("thread B is sleeping 10ms");
    36                                 Thread.sleep(10);
    37                                 System.out.println("thread B is done");
    38                             } catch (InterruptedException e) {
    39                                 e.printStackTrace();
    40                             }
    41                         }
    42                     }
    43                 }).start();// 创建B线程并启动完毕
    44     }
    45 }

    结果为:

    thread A is waiting to  get lock

    thread B is waiting to get lock

    thread A  do wait method

    thread B  get lock

    thread B is sleeping 10ms

    thread B is done

    thread A is done

    第二个例子

     1 public class SleepWaitDemo {
     2     public static void main(String[] args) {
     3         final Object lock = new Object();
     4         // 创建A线程,并启动
     5         new Thread(new Runnable() {
     6             public void run() {
     7                 System.out.println("thread A is waiting to  get lock");
     8                 synchronized (lock) {
     9                     try {
    10                         System.out.println("thread A get lock");
    11                         Thread.sleep(20);//sleep()仅释放CPU但不释放锁
    12                         System.out.println("thread A  do wait method");
    13                         Thread.sleep(1000);
    14                         System.out.println("thread A is done");
    15                     } catch (InterruptedException e) {
    16                         e.printStackTrace();
    17                     }
    18                 }
    19             }
    20         }).start();// 创建A线程并启动完毕
    21         
    22         try {
    23             Thread.sleep(10);//主线程sleep 10ms
    24         } catch (InterruptedException e1) {
    25             e1.printStackTrace();
    26         }
    27         
    28         // 创建B线程,并启动
    29                 new Thread(new Runnable() {
    30                     public void run() {
    31                         System.out.println("thread B is waiting to get lock");
    32                         synchronized (lock) {
    33                             try {
    34                                 System.out.println("thread B  get lock");
    35                                 System.out.println("thread B is sleeping 10ms");
    36                                 lock.wait(10);//wait()释放CPU和锁
    37                                 System.out.println("thread B is done");
    38                             } catch (InterruptedException e) {
    39                                 e.printStackTrace();
    40                             }
    41                         }
    42                     }
    43                 }).start();// 创建B线程并启动完毕
    44     }
    45 }

    结果:

    thread A is waiting to  get lock

    thread A get lock

    thread B is waiting to get lock

    thread A  do wait method

    thread A is done

    thread B  get lock

    thread B is sleeping 10ms

    thread B is done

  • 相关阅读:
    关于IE缓存的解决方案(HTML,JSP,ASP,PHP,C#)(转)
    ASP.NET 2.0 中的客户端脚本
    Post和Get的区别(兼谈页面间传值的方式)(转)
    委托的小例子
    JS的正则表达式
    HTTP消息头
    asp.net一个onclick的全过程(简单写一下)
    location.reload() 和 location.replace()的区别和应用
    使用 .NET Framework 2.0 在您的应用程序中支持证书(转)
    页面动态注册脚本(小技巧)
  • 原文地址:https://www.cnblogs.com/sbclmy/p/10854030.html
Copyright © 2011-2022 走看看