zoukankan      html  css  js  c++  java
  • 多线程-控制两个线程交替打印

    package datatype;
    
    public class demo {
        static final Object object = new Object();
        public static void main(String[] args) {
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i < 51; i++) {
                        System.out.print(i);
                        if (i % 2 == 0) {
                            synchronized (object) {
                                object.notify();
                                try {
                                    object.wait();
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
    
                        }
                    }
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    char word = 'A';
                    for (int i = 0; i < 27; i++) {
                        System.out.print(word);
                        word++;
                        synchronized (object) {
                            object.notify();
                            try {
                                object.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });
            t2.start();
            t1.start();
        }
    }

  • 相关阅读:
    四则运算测试脚本运行情况
    AAA
    (2015秋) 软工作业成绩公布(12月26号更新)
    判断闰年的Java算法
    B
    A
    Where Amazing Happens
    安利一发资料站
    C
    B
  • 原文地址:https://www.cnblogs.com/excellencesy/p/11828297.html
Copyright © 2011-2022 走看看