生成一些随机数,一个线程负责将随机数压到队列里,另外一个线程负责从队列取数据。请大侠编程指点
解决方案 »
- 请问:C/S模式下,可以使用连接池吗?
- 求编写一个Java Application
- 做一自动取款机的系统!并请教几个问题!
- Java中foreach的用法?(突然老师说要考这个)我本以为foreach只在C#中才有!
- 接口变量重名问题
- 求助:关于文件的复制
- Package Error
- 是不是在一个程序代码里不能有两个public 的class?那我这个怎么不能编译通过?
- 关于静态线程!!请教高手!!
- 在连接数据库JDBC中,编译通过,运行时报错显示Exception in thread"main"java.lang.noclassdefFoundError:JDBCDemo
- 对象池中对象的创建
- swing界面socket程序求修改!
package testio;import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;public class TestList { private Queue<Integer> queue = new LinkedList<Integer>(); public synchronized boolean offer(Integer i) {
return queue.offer(i);
} public synchronized Integer poll() {
return queue.poll();
} /**
* @param args
*/
public static void main(String[] args) {
TestList tl = new TestList();
new Thread(tl.new O()).start();
Thread t = new Thread(tl.new P());
t.setDaemon(true);
t.start(); } class O implements Runnable { @Override
public void run() {
Random rand = new Random();
for (int i = 0; i <= 9; i++) {
Integer interger = rand.nextInt();
System.out.println("offer " + interger);
offer(interger);
}
}
} class P implements Runnable { @Override
public void run() {
while (true) {
Integer i;
if ((i = poll()) != null)
System.out.println("poll " + i);
}
} }}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class Queue{
List<Integer> Integers = new ArrayList<Integer>();
int maxSize = 4;
int count = 0;
public Queue(){
}
public synchronized void add(Integer e){
if(Integers.size()==maxSize){
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
this.notifyAll();
if(Integers.size()<maxSize){
System.out.println(Thread.currentThread().getName()+ " " + e);
Integers.add(e);
count++;
}
}
public synchronized void remove(){
if(Integers.size()==0){
try {
this.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
this.notifyAll();
if(Integers.size()>0){
System.out.println(Thread.currentThread().getName()+" "+this.Integers.get(0));
Integers.remove(0);
}
}
public static void main(String args[]){
Queue q = new Queue();
Create c= new Create(q);
PullOut po = new PullOut(q);
new Thread(c).start();
new Thread(po).start();
}
}
class Create implements Runnable{
String name = "生产者";
private Queue q;
public Create(Queue q){
this.q = q;
}
@Override
public void run(){
int count=0;
Random r =new Random();
while(count<10){
int number = r.nextInt()%20;
q.add(number);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}
class PullOut implements Runnable{
String name = "消费者";
private Queue q;
public PullOut(Queue q){
this.q = q;
}
@Override
public void run(){
int count=0;
while(count<10){
q.remove();
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
}
}
import java.util.Queue;
public class TestQueue {
Queue queue =new LinkedList();
public TestQueue() {
Thread t1 = new Thread(new OfferThread());
Thread t2 = new Thread(new PollThread());
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("start run....");
new TestQueue();
}
class OfferThread implements Runnable{
public void run(){
int i=1000;
while(i>0){
int random = (int)(Math.random()*100);
queue.offer(random);
System.out.println("t1:");
System.out.println("add:" + random);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i--;
}
}
}
class PollThread implements Runnable{
public void run() {
int i = 1000;
while(i>0){
if(!queue.isEmpty()){
System.out.println("t2");
System.out.println("poll:" + queue.poll());
}else{
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}