1. 程式人生 > >兩個執行緒,t1,t2,列印1-100中的數字 t1奇數,t2偶數

兩個執行緒,t1,t2,列印1-100中的數字 t1奇數,t2偶數

/**
 * 兩個執行緒,print-1   print-2   分別列印print-1 1   print-2 2    print-1 3     print-2 4  依次遞增至100
 */
public class ThreadTest {
    static volatile int count = 0;
    static volatile boolean flag = false;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (count < 100) {
                if (!flag && count % 2 == 0) {
                    count++;
                    System.out.println(Thread.currentThread() + "" + count);
                    flag = true;
                }
            }
        }, "print-1");

        Thread t2 = new Thread(() -> {
            while (count < 100) {
                if (flag && count % 2 != 0) {
                    count++;
                    System.out.println(Thread.currentThread() + "" + count);
                    flag=false;
                }
            }
        },"print-2");
        t1.start();
        t2.start();
    }

}

 

列印結果:

Thread[print-1,5,main]1
Thread[print-2,5,main]2
Thread[print-1,5,main]3
Thread[print-2,5,main]4
Thread[print-1,5,main]5
Thread[print-2,5,main]6
Thread[print-1,5,main]7
Thread[print-2,5,main]8
Thread[print-1,5,main]9
Thread[print-2,5,main]10
Thread[print-1,5,main]11
Thread[print-2,5,main]12
Thread[print-1,5,main]13
Thread[print-2,5,main]14
Thread[print-1,5,main]15
Thread[print-2,5,main]16
Thread[print-1,5,main]17
Thread[print-2,5,main]18
Thread[print-1,5,main]19
Thread[print-2,5,main]20
Thread[print-1,5,main]21
Thread[print-2,5,main]22
Thread[print-1,5,main]23
Thread[print-2,5,main]24
Thread[print-1,5,main]25
Thread[print-2,5,main]26
Thread[print-1,5,main]27
Thread[print-2,5,main]28
Thread[print-1,5,main]29
Thread[print-2,5,main]30
Thread[print-1,5,main]31
Thread[print-2,5,main]32
Thread[print-1,5,main]33
Thread[print-2,5,main]34
Thread[print-1,5,main]35
Thread[print-2,5,main]36
Thread[print-1,5,main]37
Thread[print-2,5,main]38
Thread[print-1,5,main]39
Thread[print-2,5,main]40
Thread[print-1,5,main]41
Thread[print-2,5,main]42
Thread[print-1,5,main]43
Thread[print-2,5,main]44
Thread[print-1,5,main]45
Thread[print-2,5,main]46
Thread[print-1,5,main]47
Thread[print-2,5,main]48
Thread[print-1,5,main]49
Thread[print-2,5,main]50
Thread[print-1,5,main]51
Thread[print-2,5,main]52
Thread[print-1,5,main]53
Thread[print-2,5,main]54
Thread[print-1,5,main]55
Thread[print-2,5,main]56
Thread[print-1,5,main]57
Thread[print-2,5,main]58
Thread[print-1,5,main]59
Thread[print-2,5,main]60
Thread[print-1,5,main]61
Thread[print-2,5,main]62
Thread[print-1,5,main]63
Thread[print-2,5,main]64
Thread[print-1,5,main]65
Thread[print-2,5,main]66
Thread[print-1,5,main]67
Thread[print-2,5,main]68
Thread[print-1,5,main]69
Thread[print-2,5,main]70
Thread[print-1,5,main]71
Thread[print-2,5,main]72
Thread[print-1,5,main]73
Thread[print-2,5,main]74
Thread[print-1,5,main]75
Thread[print-2,5,main]76
Thread[print-1,5,main]77
Thread[print-2,5,main]78
Thread[print-1,5,main]79
Thread[print-2,5,main]80
Thread[print-1,5,main]81
Thread[print-2,5,main]82
Thread[print-1,5,main]83
Thread[print-2,5,main]84
Thread[print-1,5,main]85
Thread[print-2,5,main]86
Thread[print-1,5,main]87
Thread[print-2,5,main]88
Thread[print-1,5,main]89
Thread[print-2,5,main]90
Thread[print-1,5,main]91
Thread[print-2,5,main]92
Thread[print-1,5,main]93
Thread[print-2,5,main]94
Thread[print-1,5,main]95
Thread[print-2,5,main]96
Thread[print-1,5,main]97
Thread[print-2,5,main]98
Thread[print-1,5,main]99
Thread[print-2,5,main]100

Process finished with exit code 0