使用Java编写一个程序,启动三个线程,三个线程的ID分别是A,B,C;每个线程将自己的ID值在屏幕上打印5遍,打印顺序是ABCABC......

作为初学者,这里的大神写的很多看不懂,这个问题可以用一些比较简单的思路去解决:

直接创建三个线程类,然后通过另一个类连接起来,再去测试类开启线程;

//这是起连接作用的类,定义的num用于后期判定输出A或B或C:
public class Name {
    int num=0;
}
//输出A的类:

public   class PrintA implements Runnable{
    Name n;
    public PrintA(){}
    public PrintA(Name n){
        this.n=n;
    }

    public void run() {
        // TODO Auto-generated method stub
        int count=0;
        while(count<5){
            synchronized (n) {
                try{
                    Thread.sleep(100);
                }catch(InterruptedException e1){
                    e1.printStackTrace();
                }
                if(n.num==0){
                    System.out.println("A");
                    count++;
                    n.num=1;
                    n.notifyAll();
                    try {
                        n.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

//输出B的类

public class PrintB implements Runnable{
    Name n;
    public PrintB(){}
    public PrintB(Name n){
        this.n=n;
    }
    public void run(){
        int count=0;
        while(count<5){
            synchronized (n) {
                try{
                    Thread.sleep(100);
                }catch(InterruptedException e1){
                    e1.printStackTrace();
                }
                if(n.num==1){
                    System.out.println("B");
                    count++;
                    n.num=2;
                    n.notifyAll();
                    try {
                        n.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

//输出C的类

public class PrintC implements Runnable{
    Name n;
    public PrintC(){}
    public PrintC(Name n){
        this.n=n;
    }
    public void run(){
        int count=0;
        while(count<5){
            synchronized (n) {
                try{
                    Thread.sleep(100);
                }catch(InterruptedException e1){
                    e1.printStackTrace();
                }
                if(n.num==2){
                    System.out.println("C");
                    count++;
                    n.num=0;
                    n.notifyAll();
                    try {
                        n.wait();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

//测试类

public class Test {
    public static void main(String[] args) {
        Name n=new Name();
    
        PrintA pa=new PrintA(n);
        PrintB pb=new PrintB(n);
        PrintC pc=new PrintC(n);
        
        Thread th1=new Thread(pa);
        Thread th2=new Thread(pb);
        Thread th3=new Thread(pc);
        
        th1.start();
        th2.start();
        th3.start();
    }
    
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值