启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20.

  • Post author:
  • Post category:其他


启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20….以此类推, 直到打印到75.

//没有注释
package com.ry.test;

public class Test01 {
    public static void main(String[] args) {
        Thread t1 = new Thread(new runnable1(1));
        Thread t2 = new Thread(new runnable1(2));
        Thread t3 = new Thread(new runnable1(3));
        t1.start();
        t2.start();
        t3.start();
    }
}

class runnable1 implements Runnable {
    private int cid;
    private int sid = 0;
    static private int count = 0;

    public runnable1(int cid) {
        this.cid = cid;
    }

    @Override
    public void run() {
        while (count<75) {
            synchronized (runnable1.class) {
                sid = count / 5 % 3 + 1;
                if (sid == cid) {
                    for (int i = 0; i < 5; i++) {
                        System.out.println("线程" + cid + 



版权声明:本文为Ruyi_squad原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。