import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
*程序代码模拟产生16个日志对象,并且需要运行16秒才能打印完这些日志,请在程序中增加4个线程4s完成日志日志输入
*
*public static void main(String[] args) {
for(int i=1;i<=16;i++){
parseLog(i+"");
}
}
public static void parseLog(String log){
System.out.println(log+" "+System.currentTimeMillis()/1000);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*/
public class ThreadTest2 {
public static void main(String[] args) {
final BlockingQueue<String> bq=new ArrayBlockingQueue<String>(16);
for(int i=1;i<=4;i++){
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
try {
while(true){
parseLog(bq.take());
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
for(int i=1;i<=16;i++){
try {
bq.put(i+"");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void parseLog(String log){
System.out.println(log+" "+System.currentTimeMillis()/1000);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
java多线程:17、面试题1
最新推荐文章于 2023-08-13 18:34:10 发布