多线程编程面试题1
来源:互联网 发布:数据之巅 pdf 下载 编辑:程序博客网 时间:2024/04/29 14:59
package zhu.suo;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/*1.四个线程共用4秒打印出16条日志
*
* 解析:
* 在print方法中
* 第一秒 四个进程同时带一个数据进入
* 同样。。
* */
public class Test1 {
public static void main(String[] args) {
final BlockingQueue quee = new ArrayBlockingQueue<String>(16);
for (int i=1;i<5;i++)
{
new Thread(
new Runnable() {
@Override
public void run() {
while(true)
try {
print(quee.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
}
for (int i=1;i<17;i++)
{
String s = "i:"+i;
try {
quee.put(s);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static void print(Object log)
{
System.out.println("current:"+System.currentTimeMillis()/1000+",log="+log);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/*1.四个线程共用4秒打印出16条日志
*
* 解析:
* 在print方法中
* 第一秒 四个进程同时带一个数据进入
* 同样。。
* */
public class Test1 {
public static void main(String[] args) {
final BlockingQueue quee = new ArrayBlockingQueue<String>(16);
for (int i=1;i<5;i++)
{
new Thread(
new Runnable() {
@Override
public void run() {
while(true)
try {
print(quee.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
).start();
}
for (int i=1;i<17;i++)
{
String s = "i:"+i;
try {
quee.put(s);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
static void print(Object log)
{
System.out.println("current:"+System.currentTimeMillis()/1000+",log="+log);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
0 0
- 多线程编程面试题1
- 多线程编程面试题
- 多线程编程面试题2
- 多线程编程面试题3
- 多线程之编程面试题
- 多线程面试题1
- 多线程编程4-------面试题增加信号
- 多线程编程4---面试题解法3
- 多线程编程10.1-----Condition解决面试题
- 多线程编程之三个面试题
- 15个顶级java多线程编程面试题
- 多线程编程4------面试题使用资源互斥类AtomicInteger
- 空中网多线程面试题1
- java多线程:17、面试题1
- 多线程笔试面试题(1)
- java多线程面试题
- 多线程面试题
- 多线程面试题
- C++中简单运算符的重载代码
- PostgreSQL对象重组工具【pg_reorg】
- u-boot下配置挂载NFS根文件系统
- 交叉编译busybox
- 1763 A Simple Question of Chemistry
- 多线程编程面试题1
- C++的I/O操作
- C++实现求两个字符串的最大公共子串
- 关于用移动硬盘装系统的几个问题
- 字符串复制
- SpringMVC 无法使用aop拦截的解决方案
- NYOJ119 士兵杀敌(三)&&poj3264 Balanced Lineup(线段树)
- 开源中国客户端源码下载地址
- 小黑小波比.node的REPL模式的退出