zoukankan      html  css  js  c++  java
  • 多线程案例1:奇偶交替输出

    尝试写了一个奇偶交替输出的demo,记录一下问题:

    原来的线程类是如下:

     1 package com.future.day0521;
     2 
     3 public class demo1 {
     4     public static int num = 1;
     5     public static Object js = new Object();
     6     public static Object os = new Object();
     7 
     8     static class odd implements Runnable{
     9 
    10         public void run() {
    11             while (true){
    12                 synchronized (js){
    13                     synchronized (os){
    14                         System.out.println("odd:"+num);
    15                         num++;
    16                         os.notifyAll();
    17                         try {
    18                             js.wait();
    19                         } catch (InterruptedException e) {
    20                             e.printStackTrace();
    21                         }
    22                     }
    23                 }
    24                 Thread.yield();
    25             }
    26         }
    27     }
    28 
    29     static class even implements Runnable{
    30 
    31         public void run() {
    32             while (true){
    33                 synchronized (os){
    34                     synchronized (js){
    35                         System.out.println("even:"+num);
    36                         num++;
    37                         js.notifyAll();
    38                         try {
    39                             os.wait();
    40                         } catch (InterruptedException e) {
    41                             e.printStackTrace();
    42                         }
    43                     }
    44                 }
    45                 Thread.yield();
    46             }
    47         }
    48     }
    49 
    50     public static void main(String[] args) throws InterruptedException {
    51         Thread t1 = new Thread(new odd());
    52         Thread t2 = new Thread(new even());
    53         t1.start();
    54         Thread.sleep(10);
    55         t2.start();
    56     }
    57 }
    View Code

    运行结果是,卡在了odd:1,使用visualvm软件,看到dump文件中是even线程blocking了,卡在了获取js的锁上,然后把odd类修改一下

    static class odd implements Runnable{
            public void run() {
                while (true){
                    synchronized (js){
                        synchronized (os){
                            System.out.println("odd:"+num);
                            num++;
                            os.notifyAll();
                        }
                        try {
                            js.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    Thread.yield();
                }
            }
        }
    View Code

    将js.wait移动到外面,代码就可以正常执行了。深入原因没有考虑,后面有时间研究一下

  • 相关阅读:
    JSP介绍
    Cookie和Session的原理及异同
    过滤器的使用
    JAVA 集合
    **JVM** 类加载机制
    *JAVA IO/NIO*
    jvm
    ioc
    spring问题总结
    aop详解
  • 原文地址:https://www.cnblogs.com/tianphone/p/10904107.html
Copyright © 2011-2022 走看看