小编van*_*nza的帖子

wait()是"if"块或"while"块

以下代码复制自http://www.programcreek.com/2009/02/notify-and-wait-example/

我看过很多使用while循环来包装wait()的例子

我的问题:在我第一次尝试解决类似的问题时,我使用if语句来包装wait().例如,

if(messages.size() == MAXQUEUE) {
        wait();
    }
Run Code Online (Sandbox Code Playgroud)

使用while循环而不是if语句有什么好处?

import java.util.Vector;

class Producer extends Thread {

    static final int MAXQUEUE = 5;
    private Vector messages = new Vector();

    @Override
    public void run() {
        try {
            while (true) {
                putMessage();
                //sleep(5000);
            }
        } catch (InterruptedException e) {
        }
    }

    private synchronized void putMessage() throws InterruptedException {
        while (messages.size() == MAXQUEUE) {
            wait();
        }
        messages.addElement(new java.util.Date().toString());
        System.out.println("put message");
        notify();
        //Later, when the necessary event happens, the thread that is running …
Run Code Online (Sandbox Code Playgroud)

java multithreading synchronized wait

4
推荐指数
1
解决办法
2328
查看次数

C中的跨平台数据类型库

是否有用于提供跨平台数据类型的C(不是C++,Boost等)库?

详细说来,我想将int一个文件存储在一个32位的linux机器中,int从64位的Windows机器上读取该文件.这可能吗 ?

更新:我也不想使用sqlite或某种数据库.我想要一个可以帮助我在整个代码中使用数据类型的库.

c cross-platform

0
推荐指数
1
解决办法
424
查看次数

标签 统计

c ×1

cross-platform ×1

java ×1

multithreading ×1

synchronized ×1

wait ×1