如果用两个线程类来实现的话,应该很容易写的,参考生产者与消费者的例子就OK了。我写了只使用一个线程类来实现的交替打印,供参考,不过有个小问题,我用了sleep来控制了每一段(10个)双数和单数的打印先后,可能各位大虾会有更好的方法。代码如下:import java.util.concurrent.CyclicBarrier;public class printThread extends Thread { int numberic; boolean isEven; private CyclicBarrier barrier; public printThread(CyclicBarrier barrier,boolean isEven, int numberic) { this.barrier = barrier; this.isEven = isEven; //true,双数打印;false,单数打印 this.numberic = numberic;//从什么值,开始连续打印50个双(单)数 } public synchronized int printNumberic(int numberic) { int count = 0; if (isEven) { System.out.print("从{ "+numberic+" }开始,递增输出一组偶数:"); } else { System.out.print("从{ "+numberic+" }开始,递增输出一组奇数:"); } while (count < 10) { System.out.print(numberic + ","); numberic = numberic + 2; count = count + 1; } return numberic; } public void run() { try { while (numberic < 100) { if (!isEven){ sleep(2000);//先打印双数,利用sleep()做出时间间隔,后打印单数 }else{ sleep(1000);//为演示效果,控制打印双数的时间间隔 } numberic = this.printNumberic(numberic); System.out.println(); barrier.await();//使用barrier保证每打印一组双数和一组单数后,继续递增式打印 } } catch (Exception ex) { ex.printStackTrace(); } } } 写个Main函数 public static void main(String[] args) throws Exception { // TODO Auto-generated method stub //SubListGenericFoo<HashMap, ArrayList> o = new SubListGenericFoo<HashMap, ArrayList>(); CyclicBarrier barrier = new CyclicBarrier(2); new printThread(barrier,true, 0).start(); new printThread(barrier,false, 1).start(); }输出结果: 从{ 0 }开始,递增输出一组偶数:0,2,4,6,8,10,12,14,16,18, 从{ 1 }开始,递增输出一组奇数:1,3,5,7,9,11,13,15,17,19, 从{ 20 }开始,递增输出一组偶数:20,22,24,26,28,30,32,34,36,38, 从{ 21 }开始,递增输出一组奇数:21,23,25,27,29,31,33,35,37,39, 从{ 40 }开始,递增输出一组偶数:40,42,44,46,48,50,52,54,56,58, 从{ 41 }开始,递增输出一组奇数:41,43,45,47,49,51,53,55,57,59, 从{ 60 }开始,递增输出一组偶数:60,62,64,66,68,70,72,74,76,78, 从{ 61 }开始,递增输出一组奇数:61,63,65,67,69,71,73,75,77,79, 从{ 80 }开始,递增输出一组偶数:80,82,84,86,88,90,92,94,96,98, 从{ 81 }开始,递增输出一组奇数:81,83,85,87,89,91,93,95,97,99,
http://www.javaeye.com/topic/806990 这篇文章能找到你要的答案
public class Test { public static void main(String[] args) { PrintNum a=new PrintNum(); Thread t1=new Thread(a,"t1"); Thread t2=new Thread(a,"t2"); t1.start(); t2.start(); } } public class PrintNum implements Runnable { private int count=1; private int odd=1; private int even=2;
public void run() { synchronized(this){ while(count<100){
if (count % 2==0){ for(int i=1;i<11;){ notify(); if (count>100) break; System.out.println(Thread.currentThread().getName()+":"+count); count+=2; even+=2; i++; if (even>100){ Thread.currentThread().interrupt(); break; } if (i==11) try { count=odd; wait(); } catch (InterruptedException e) { e.printStackTrace(); } } }
if (count % 2!=0){ for(int i=1;i<11;){ notify(); if (count>100) break; System.out.println(Thread.currentThread().getName()+":"+count); count+=2; odd+=2; i++; if (odd>99){ Thread.currentThread().interrupt(); break; } if (i==11) try { count=even; wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }}
找个java的生产者消费者实现看看,改改就行了。
for exampleclass Counter { public static void main(String[] args) { Counter counter = new Counter(); Thread t1 = new CounterThread(counter, 0); Thread t2 = new CounterThread(counter, 1); t1.start(); t2.start(); }
public synchronized void countEven() { try { for (int i=2; i<=100; i+=2) { System.out.printf("%d,", i); if (i%20 == 0) { System.out.println(); notifyAll(); if (i<100) {wait();} } } } catch (Exception e) { e.printStackTrace(); } }
public synchronized void countOdd() { try { for (int i=1; i<100; i+=2) { System.out.printf("%d,", i); if ((i+1)%20 == 0) { System.out.println(); notifyAll(); if (i<99) {wait();} } } } catch (Exception e) { e.printStackTrace(); } } }class CounterThread extends Thread { Counter counter; int flag; public CounterThread(Counter counter, int flag) { this.counter = counter; this.flag = flag; }
public void run() { if (flag%2 == 0) { counter.countEven(); } else { counter.countOdd(); } } }
int numberic;
boolean isEven;
private CyclicBarrier barrier; public printThread(CyclicBarrier barrier,boolean isEven, int numberic) {
this.barrier = barrier;
this.isEven = isEven; //true,双数打印;false,单数打印
this.numberic = numberic;//从什么值,开始连续打印50个双(单)数
} public synchronized int printNumberic(int numberic) {
int count = 0;
if (isEven) {
System.out.print("从{ "+numberic+" }开始,递增输出一组偶数:");
} else {
System.out.print("从{ "+numberic+" }开始,递增输出一组奇数:");
}
while (count < 10) {
System.out.print(numberic + ",");
numberic = numberic + 2;
count = count + 1;
}
return numberic; } public void run() {
try {
while (numberic < 100) {
if (!isEven){
sleep(2000);//先打印双数,利用sleep()做出时间间隔,后打印单数
}else{
sleep(1000);//为演示效果,控制打印双数的时间间隔
}
numberic = this.printNumberic(numberic);
System.out.println();
barrier.await();//使用barrier保证每打印一组双数和一组单数后,继续递增式打印
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
写个Main函数 public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//SubListGenericFoo<HashMap, ArrayList> o = new SubListGenericFoo<HashMap, ArrayList>();
CyclicBarrier barrier = new CyclicBarrier(2);
new printThread(barrier,true, 0).start();
new printThread(barrier,false, 1).start(); }输出结果:
从{ 0 }开始,递增输出一组偶数:0,2,4,6,8,10,12,14,16,18,
从{ 1 }开始,递增输出一组奇数:1,3,5,7,9,11,13,15,17,19,
从{ 20 }开始,递增输出一组偶数:20,22,24,26,28,30,32,34,36,38,
从{ 21 }开始,递增输出一组奇数:21,23,25,27,29,31,33,35,37,39,
从{ 40 }开始,递增输出一组偶数:40,42,44,46,48,50,52,54,56,58,
从{ 41 }开始,递增输出一组奇数:41,43,45,47,49,51,53,55,57,59,
从{ 60 }开始,递增输出一组偶数:60,62,64,66,68,70,72,74,76,78,
从{ 61 }开始,递增输出一组奇数:61,63,65,67,69,71,73,75,77,79,
从{ 80 }开始,递增输出一组偶数:80,82,84,86,88,90,92,94,96,98,
从{ 81 }开始,递增输出一组奇数:81,83,85,87,89,91,93,95,97,99,
public static void main(String[] args) {
PrintNum a=new PrintNum();
Thread t1=new Thread(a,"t1");
Thread t2=new Thread(a,"t2");
t1.start();
t2.start();
}
}
public class PrintNum implements Runnable {
private int count=1;
private int odd=1;
private int even=2;
public void run() {
synchronized(this){
while(count<100){
if (count % 2==0){
for(int i=1;i<11;){
notify();
if (count>100) break;
System.out.println(Thread.currentThread().getName()+":"+count);
count+=2;
even+=2;
i++;
if (even>100){
Thread.currentThread().interrupt();
break;
}
if (i==11)
try {
count=odd;
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (count % 2!=0){
for(int i=1;i<11;){
notify();
if (count>100) break;
System.out.println(Thread.currentThread().getName()+":"+count);
count+=2;
odd+=2;
i++;
if (odd>99){
Thread.currentThread().interrupt();
break;
}
if (i==11)
try {
count=even;
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} }
} }}
public static void main(String[] args) {
Counter counter = new Counter();
Thread t1 = new CounterThread(counter, 0);
Thread t2 = new CounterThread(counter, 1);
t1.start();
t2.start();
}
public synchronized void countEven() {
try {
for (int i=2; i<=100; i+=2) {
System.out.printf("%d,", i);
if (i%20 == 0) {
System.out.println();
notifyAll();
if (i<100) {wait();}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void countOdd() {
try {
for (int i=1; i<100; i+=2) {
System.out.printf("%d,", i);
if ((i+1)%20 == 0) {
System.out.println();
notifyAll();
if (i<99) {wait();}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}class CounterThread extends Thread {
Counter counter;
int flag;
public CounterThread(Counter counter, int flag) {
this.counter = counter;
this.flag = flag;
}
public void run() {
if (flag%2 == 0) {
counter.countEven();
} else {
counter.countOdd();
}
}
}