package com.atguigu;
public class printTest {
volatile static boolean open=false;
volatile static int index=0;
static Object ob=new Object();
static Integer i=0;
static Thread t1=new Thread(new myRun1(),"线程1");
static Thread t2=new Thread(new myRun2(),"线程2");
public static void main(String[] args){
t1.start();
t2.start();
}
static class myRun1 implements Runnable{
@Override
public void run() {
while(true){
synchronized (ob) {
if(open){
System.out.println(t1.getName()+ " "+i+"a");
i++;
open=false;
}else{
ob.notifyAll();
try
两个线程循环打印ab
最新推荐文章于 2024-04-13 00:14:11 发布