N***m 发帖数: 4460 | 1 [题目]
设计两个线程类,一个线程类负责打印100以内所有的偶数,另一个线程打印100以内所
有的奇数。要求偶数线程每打印10个偶数以后,就让奇数线程打印10个奇数,如此交替
进行。
=======================================================================
有人给了CyclicBarrier的解法,不知道这是啥咚咚。自己编的似乎也能运行,
就是不知道和CyclicBarrier的解法有啥差距?
=======================================================================
我的解法很直接。
[PrintEvenRunnable.java] 还有类似的PrintOddRunnable;
public class PrintEvenRunnable implements Runnable {
private PrintStatus status;
private static int n = 0;
public PrintEvenRunnable(PrintStatus status) {
this.status = status;
}
@Override
public void run() {
while(n<=4){
if(status.getFlag()=='e') {
for(int i=n*20+2;i<=(n+1)*20;i+=2)
System.out.print(i+" ");
System.out.println();
n++;
status.setFlag('o');
}
}
}
}
[PrintStatus.java]
public class PrintStatus {
private char flag;
public synchronized char getFlag() {
return flag;
}
public synchronized void setFlag(char flag) {
this.flag = flag;
}
}
[Main.java]
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintStatus status = new PrintStatus();
status.setFlag('e');
new Thread(new PrintEvenRunnable(status)).start();
new Thread(new PrintOddRunnable(status)).start();
}
} |
t****t 发帖数: 6806 | 2 你这个是用poll的吧, 哪能这么写程序.
【在 N***m 的大作中提到】 : [题目] : 设计两个线程类,一个线程类负责打印100以内所有的偶数,另一个线程打印100以内所 : 有的奇数。要求偶数线程每打印10个偶数以后,就让奇数线程打印10个奇数,如此交替 : 进行。 : ======================================================================= : 有人给了CyclicBarrier的解法,不知道这是啥咚咚。自己编的似乎也能运行, : 就是不知道和CyclicBarrier的解法有啥差距? : ======================================================================= : 我的解法很直接。 : [PrintEvenRunnable.java] 还有类似的PrintOddRunnable;
|
N***m 发帖数: 4460 | 3 弱问一下,poll是什么啊?
我知道我的办法土,但是真用起来会有什么问题?
【在 t****t 的大作中提到】 : 你这个是用poll的吧, 哪能这么写程序.
|
l********f 发帖数: 149 | 4 楼主有没有运行过啊?
1. waste cpu cycle to polling status flag, use wait/notify instead
2. code doesn't work... you check n value 4 times, and it may out print out
anything!
3. BTW, I don't think cycliBarrier will work. there is no way you can
control the sequence of printing odd/event number with this synchronizer. |
g*****g 发帖数: 34805 | 5 CyclicBarrier will work. Let odd number thread await in the
beginning and the 2 threads can do the printing in turns. Probably
cleaner than wait/notify. With wait/notify you still need to use
a shared atomic boolean, otherwise there's no way to guarrantee
even thread will start printing first.
The 2 work in the same way internally.
out
【在 l********f 的大作中提到】 : 楼主有没有运行过啊? : 1. waste cpu cycle to polling status flag, use wait/notify instead : 2. code doesn't work... you check n value 4 times, and it may out print out : anything! : 3. BTW, I don't think cycliBarrier will work. there is no way you can : control the sequence of printing odd/event number with this synchronizer.
|
N***m 发帖数: 4460 | 6
out
你是说n=4的情形?我运行过,没问题啊。
【在 l********f 的大作中提到】 : 楼主有没有运行过啊? : 1. waste cpu cycle to polling status flag, use wait/notify instead : 2. code doesn't work... you check n value 4 times, and it may out print out : anything! : 3. BTW, I don't think cycliBarrier will work. there is no way you can : control the sequence of printing odd/event number with this synchronizer.
|
g**e 发帖数: 6127 | 7 好虫说说看CyclicBarrier这里怎么用的?没想明白。CyclicBarrier参数至少是2吧,
怎么保证两个线程交替打印?
这里用semaphore不是简单点
【在 g*****g 的大作中提到】 : CyclicBarrier will work. Let odd number thread await in the : beginning and the 2 threads can do the printing in turns. Probably : cleaner than wait/notify. With wait/notify you still need to use : a shared atomic boolean, otherwise there's no way to guarrantee : even thread will start printing first. : The 2 work in the same way internally. : : out
|
g**e 发帖数: 6127 | 8 用semaphore写了一个,谁能告诉我用cyclicbarrier怎么做?
public class PrintOddEvenNumbersInTurn {
public static void main(String[] args) {
Semaphore mutex = new Semaphore(1);
Semaphore mutex2 = new Semaphore(1);
try {
mutex.acquire();
mutex2.acquire();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
NumPrinter OddPrinter = new NumPrinter(mutex, mutex2, 1);
NumPrinter EvenPrinter = new NumPrinter(mutex2, mutex, 2);
OddPrinter.start();
EvenPrinter.start();
mutex.release(); //start printing odd number first
}
}
class NumPrinter extends Thread {
private Semaphore mutex;
private Semaphore mutex2;
private int n;
public NumPrinter (Semaphore m, Semaphore m2, int n){
this.mutex = m;
this.mutex2 = m2;
this.n = n;
}
public void run(){
while (n<=100){
try {
mutex.acquire();
printNextTenNumber();
mutex2.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void printNextTenNumber() {
System.out.print(Thread.currentThread() + ": ");
for (int i=0; i<10; i++){
System.out.print(n + " ");
n+=2;
}
System.out.println();
}
}
【在 g**e 的大作中提到】 : 好虫说说看CyclicBarrier这里怎么用的?没想明白。CyclicBarrier参数至少是2吧, : 怎么保证两个线程交替打印? : 这里用semaphore不是简单点
|
|