package com.habby.test.test2;
public class HabbyTest {
public static int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized(array) {
for (int i = 0; i < array.length; i += 2) {
System.out.println("Thread even: " + array[i]);
array.notify();
try {
array.wait();
} catch (InterruptedException e) {
}
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (array) {
for (int i = 1; i < array.length; i += 2) {
System.out.println("Thread odd: " + array[i]);
array.notify();
try {
array.wait();
} catch (InterruptedException e) {
}
}
}
}
}).start();
}
}
打印一个数组中的数字,2个线程,一个打印奇数,一个打印偶数,交叉打印结果。
最新推荐文章于 2021-01-19 18:12:50 发布