boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 编程题又一道
相关主题
java里用synchronized包住block就可以保护多线程同步问题了,这就是c里面的mutex吧?
Restaurant Reservation System...
pthread mutex能不能用与thread和process之间
问题一枚
为什么说semaphore是一种进程间的通信方式。
about critical section
how to answer this question, thanks
mutex和semaphore的差别到底是什么?
谁能推荐一个read-writer lock的C++实现? (转载)
精华区翻出来的MS老题,thread safe
相关话题的讨论汇总
话题: semaphore话题: public话题: numprinter话题: private
进入Programming版参与讨论
1 (共1页)
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不是简单点

1 (共1页)
进入Programming版参与讨论
相关主题
精华区翻出来的MS老题,thread safe
Monitor和semaphore, mutex是什么关系?
问个semaphore 和 mutex的问题
question about the read/write locker
c++是不是准备加一个glue layer把系统给隔离出来?
问一个读写锁的问题
连续release mutex/semphore 2次有什么问题吗?
Multi-thread可以mutex锁资源,Multi-process怎么锁资源?
C++ InitializeCriticalSection问题
mutex一问
相关话题的讨论汇总
话题: semaphore话题: public话题: numprinter话题: private