以下代码复制自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) 是否有用于提供跨平台数据类型的C(不是C++,Boost等)库?
详细说来,我想将int一个文件存储在一个32位的linux机器中,int从64位的Windows机器上读取该文件.这可能吗 ?
更新:我也不想使用sqlite或某种数据库.我想要一个可以帮助我在整个代码中使用数据类型的库.