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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© gaojiangjian 中级黑马   /  2016-6-26 22:10  /  495 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//设置2个线程,t1,t2
//设置一个标记,flag=1;执行到t1的时候判断flag==1,成立就执行--操作,修改flag=0并this.notify唤醒其他线程
//不成立就执行wait()方法进入等待状态,执行到t2的时候判断flag是否为0,为0就执行--操作,修改flag=1并this.notify唤醒其他线程
//由于flag初始值为1,所以t1会先执行,然后t1,t2交替执行

2 个回复

倒序浏览
  1. package com.itheima;

  2. public class Text24 {
  3.         // static int num = 100;

  4.         public static void main(String[] args) throws Exception {
  5.                 final Printer p = new Printer();

  6.                 new Thread() {

  7.                         public void run() {
  8.                                 while (true) {
  9.                                         try {
  10.                                                 p.print1();

  11.                                         } catch (Exception e) {

  12.                                                 e.printStackTrace();
  13.                                         }
  14.                                 }
  15.                         }
  16.                 }.start();

  17.                 new Thread() {

  18.                         public void run() {
  19.                                 while (true) {
  20.                                         try {
  21.                                                 p.print2();

  22.                                         } catch (Exception e) {

  23.                                                 e.printStackTrace();
  24.                                         }
  25.                                 }
  26.                         }
  27.                 }.start();

  28.         }

  29.         static class Printer {
  30.                 private int flag = 1;
  31.                 private int num = 1;

  32.                 public void print1() throws Exception {
  33.                         while (true) {
  34.                                 synchronized (this) {
  35.                                         Thread.sleep(1000);
  36.                                         if (flag != 1) {
  37.                                                 this.wait();
  38.                                         }
  39.                                         // if (num > 100) {
  40.                                         // break;
  41.                                         // }
  42.                                         System.out.println("A线程打印" + num++);
  43.                                         flag = 2;
  44.                                         this.notify();
  45.                                 }
  46.                         }
  47.                 }

  48.                 public void print2() throws Exception {
  49.                         while (true) {
  50.                                  synchronized (this) {
  51.                                 Thread.sleep(1000);
  52.                                 if (flag != 2) {
  53.                                         this.wait();
  54.                                 }
  55.                                 // if (num > 100) {
  56.                                 // break;
  57.                                 // }
  58.                                 System.out.println("B线程打印" + num++);
  59.                                 flag = 1;
  60.                                 this.notify();
  61.                                  }
  62.                         }
  63.                 }

  64.         }
  65. }
复制代码
回复 使用道具 举报
谢谢分享
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马