好难的线程题
时间:2008-05-10 09:57:15
来源:论坛整理 作者: 编辑:chinaitzhe
网友回复:启动两线程 一个有空位时才添加 一个满时才清空 代码懒得写了
网友回复:写了一个,根据你的要求,只是我让他满的时候就停了,可以根据自己的需求修改
- Java code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ 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
网友回复: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() ":队列已满,清空队列");
}
}
}
}
网友回复:LS两位还要多看下线程的机制。。。
网友回复:这个有啥子难得 用wait() 和notify() 就可以了
void run()
{
while(true)
{
if(满)
{
thread2.notify();
wait();
}
else
{ 添加}
}
}
synchronized void clear()
{
this.notify();
}
另外一个线程同理有wait() 和notify() 在另外一个线程中执行了清空后直接调用thead1.clear()
网友回复:。。。楼上写的真简单啊。
网友回复:这不就是典型的生产者-消费者问题吗,我写了个,楼主看看
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;
}
}
网友回复:该回复于2008-05-01 05:13:27被版主删除
关键字:线程,
上一篇:好难的数组题目
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面