写一个生产者,一个消费者,一个同步队列。
生产者不停的向同步队列写整数,消费者取整数并打印。示例如下:import java.*;
import java.util.*;
import java.lang.*;
import java.io.*;public class test {
public test() { } void go() {
SynQueue queue = new SynQueue();
Thread tp = new Thread(new Producer(queue));
Thread tc = new Thread(new Consumer(queue,System.out));
tp.start();
tc.start();
} public static void main(String[] args) {
test t = new test();
t.go();
}
}class SynQueue {
private LinkedList list = new LinkedList(); synchronized void add(int a) {
java.lang.Integer i = new Integer(a);
list.addLast(i);
} synchronized int pop() {
if (list.isEmpty()) {
return 0;
}
Integer i = (Integer) list.getFirst();
list.removeFirst();
return i.intValue();
} synchronized boolean isEmpty() {
return list.isEmpty();
}
}class Producer
implements Runnable {
private SynQueue queue;
java.util.Random rdm = new Random(); Producer(SynQueue queue) {
this.queue = queue;
} public void run() {
while (true) {
int a = rdm.nextInt() % 100;
queue.add(a);
if (a == 0) {
return;
}
try {
Thread.sleep(10);
}catch (Exception e) {}
}
}
}class Consumer implements Runnable {
private SynQueue queue;
OutputStream os;
Consumer(SynQueue queue,OutputStream os) {
this.queue = queue;
this.os = os;
}
public void run() {
java.io.OutputStreamWriter bos = new OutputStreamWriter(os);
while(true) {
while(!queue.isEmpty()) {
int a = queue.pop();
try {
bos.write("" + a + "\n");
if (a == 0) {
bos.write("exit.");
bos.flush();
return;
}
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
try {
bos.flush();
Thread.sleep(100);
}catch (Exception e) {}
}
}
}
生产者不停的向同步队列写整数,消费者取整数并打印。示例如下:import java.*;
import java.util.*;
import java.lang.*;
import java.io.*;public class test {
public test() { } void go() {
SynQueue queue = new SynQueue();
Thread tp = new Thread(new Producer(queue));
Thread tc = new Thread(new Consumer(queue,System.out));
tp.start();
tc.start();
} public static void main(String[] args) {
test t = new test();
t.go();
}
}class SynQueue {
private LinkedList list = new LinkedList(); synchronized void add(int a) {
java.lang.Integer i = new Integer(a);
list.addLast(i);
} synchronized int pop() {
if (list.isEmpty()) {
return 0;
}
Integer i = (Integer) list.getFirst();
list.removeFirst();
return i.intValue();
} synchronized boolean isEmpty() {
return list.isEmpty();
}
}class Producer
implements Runnable {
private SynQueue queue;
java.util.Random rdm = new Random(); Producer(SynQueue queue) {
this.queue = queue;
} public void run() {
while (true) {
int a = rdm.nextInt() % 100;
queue.add(a);
if (a == 0) {
return;
}
try {
Thread.sleep(10);
}catch (Exception e) {}
}
}
}class Consumer implements Runnable {
private SynQueue queue;
OutputStream os;
Consumer(SynQueue queue,OutputStream os) {
this.queue = queue;
this.os = os;
}
public void run() {
java.io.OutputStreamWriter bos = new OutputStreamWriter(os);
while(true) {
while(!queue.isEmpty()) {
int a = queue.pop();
try {
bos.write("" + a + "\n");
if (a == 0) {
bos.write("exit.");
bos.flush();
return;
}
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
try {
bos.flush();
Thread.sleep(100);
}catch (Exception e) {}
}
}
}
解决方案 »
- 坦克大战中按键监听问题。
- 新手ajax 调用问题
- struts2实现用户的管理的设计的疑问
- 关于SSH集成时的版本冲突问题
- hibernate如何执行自定的hql语句
- 请教几个structs标签的问题
- 如何为ibatis中调用的存储过程的参数赋值
- 开源纯JAVA全文搜索引擎,希望大家给点意见。
- 已实现文本文件的读取,现在想去掉多余和代码部分!应该用什么代码来实现,各位请帮帮忙,在线等,谢谢!
- Spring Boot 问题大家帮忙看一看(JavaEE 开发的颠覆者 spring boot 实战 )
- 谁有 JAVA2 API 大全的电子文档下载地址
- 老长时间上不来了, 终于上来, 高兴散分, 同时提问: 我在tomcat5下如何设立一个myapp这样的虚ni目录, 高分
但楼上的程序有一处需稍作修改:
要求的是0到1000之间的随机数,所以要把int a = rdm.nextInt() % 100;
改为:
int a = (Math.abs(rdm.nextInt())) % 1000;
但楼上的程序有一处需稍作修改:
要求的是0到1000之间的随机数,所以要把int a = rdm.nextInt() % 100;
改为:
int a = (Math.abs(rdm.nextInt())) % 1000;
但楼上的程序有一处需稍作修改:
要求的是0到1000之间的随机数,所以要把int a = rdm.nextInt(1000);
hehe;