本帖最后由 陶智第 于 2013-10-6 17:01 编辑
package read;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class Test {
public static void main(String[] args){
final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(16);
for(int x=0; x<4; x++)
{
new Thread()
{
public void run()
{
while(true) //这个地方的while语句的作用是什么?
{
try
{
String log1 = queue.take();
Test.parseLog(log1);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
System.out.println("begin:"+(System.currentTimeMillis()/1000));
for(int i=0;i<16;i++){
final String log = ""+(i+1);
{
//Test.parseLog(log);
try
{
queue.put(log);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void parseLog(String log){
System.out.println(Thread.currentThread().getName()+":"+log+":"+(System.currentTimeMillis()/1000));
try { //这里为什么要让程序休眠一秒钟
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
上面两个问题看视频的时候不太清楚。
|