您的位置:首页 > 其它

打印一个数组中的数字,2个线程,一个打印奇数,一个打印偶数,交叉打印结果。

2014-05-29 20:13 441 查看
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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐