好难的线程题

lc2tp 2008-04-13 10:28:14
同时启动2个线程 声明一个队列 当队列为空时,一个线程向队列李添加元素,当队列满了,另一个线程清空队列!要求用代码写出??
...全文
88 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
roc230 2008-04-14
  • 打赏
  • 举报
回复
package aaa;

import java.util.LinkedList;

public class ThreadTest {

protected static LinkedList linkedList = new LinkedList ();

public static void main(String[]args){
Add add = new Add();
Del del = new Del();
Thread t = new Thread(add);
t.start();
t = new Thread(del);
t.start();
}
}
class Add implements Runnable{

public synchronized void run() {
while(true){
if(ThreadTest.linkedList.size() < 10){
ThreadTest.linkedList.addLast("1");
System.out.println(Thread.currentThread().getName() + ":当前队列大小" + ThreadTest.linkedList.size());
}
}
}

}
class Del implements Runnable{

public synchronized void run() {
while(true){
if(ThreadTest.linkedList.size() == 10){ //假设队列大小为10
ThreadTest.linkedList.clear();
System.out.println(Thread.currentThread().getName() + ":队列已满,清空队列");
}
}

}

}
SBtoSB 2008-04-14
  • 打赏
  • 举报
回复
LS两位还要多看下线程的机制。。。
whmjxa 2008-04-14
  • 打赏
  • 举报
回复
这不就是典型的生产者-消费者问题吗,我写了个,楼主看看
public class Test {
public static void main(String[] args) {
Queue q=new Queue();
Producer p=new Producer(q);
Customer c=new Customer(q);
new Thread(p).start();
new Thread(c).start();
}
}
class Producer implements Runnable{
Queue q;
public Producer(Queue q){
this.q=q;
}
public void run() {
for(int i=0;i<10;i++){ //这里按需要自己改
q.put(i);
System.out.println("Producer puts:"+i);
}
}
}
class Customer implements Runnable{
Queue q;
public Customer(Queue q){
this.q=q;
}
public void run() {
while(true){
System.out.println("Customer gets:"+q.get());
}
}
}
class Queue{

private int value;
private boolean full=false;
public synchronized void put(int i){
if(!full){
value=i;
full=true;
notify();
}
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public synchronized int get(){
if(!full){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
full=false;
notify();
return value;
}
}
kuria001 2008-04-14
  • 打赏
  • 举报
回复
。。。楼上写的真简单啊。
liluyemin 2008-04-14
  • 打赏
  • 举报
回复
这个有啥子难得 用wait() 和notify() 就可以了
void run()
{
while(true)
{
if(满)
{
thread2.notify();
wait();

}
else
{ 添加}

}
}

synchronized void clear()
{
this.notify();
}
另外一个线程同理有wait() 和notify() 在另外一个线程中执行了清空后直接调用thead1.clear()
  • 打赏
  • 举报
回复
写了一个,根据你的要求,只是我让他满的时候就停了,可以根据自己的需求修改


import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Test {
public static void main(String[] args) {

int length = 5; // 假设5个就满
List<Long> list = new ArrayList<Long>(length);

thread1(list,length);
thread2(list,length);
}

public static void thread1(final List<Long> list, final int length) {
new Thread() {
public void run() {
while (list.size() < length) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(System.currentTimeMillis()); // 每隔一秒存一个时间,直到存满为止
System.out.println("放入一个元素");
}
}
}.start();
}

public static void thread2(final List<Long> list, final int length) {
new Thread() {
public void run() {
boolean over = true;
while (over) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (list.size() >= length) {
System.out.println("list满,清空开始");
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
list.removeAll(list);
System.out.println("清空list,list大小为:"+list.size());
over = false;
}
else{
System.out.println("没有满");
}
}
}
}.start();
}
}



为了体现异步性,我每隔1秒存一个时间,每隔0.7秒检查有没有满,运行结果如下:
没有满
放入一个元素
没有满
放入一个元素
没有满
没有满
放入一个元素
没有满
放入一个元素
没有满
没有满
放入一个元素
list满,清空开始
1208099198575
1208099199575
1208099200575
1208099201575
1208099202575
清空list,list大小为:0

dracularking 2008-04-13
  • 打赏
  • 举报
回复
启动两线程 一个有空位时才添加 一个满时才清空 代码懒得写了

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧