问个继续线程的问题,有点迷惑。

jyf7356759 2008-03-18 08:24:30
我现在这个线程的run是这样的,如下代码:

public void run() {
while (isrun) {
for (int i = 0; i < crawledUrls.size(); i++) {
//while (isrun) {
try {
if (pause) {
synchronized (this) {
wait();
}
}
do sth
//thread sleep every 0.2 sencond, max number of link crawled within one second will be 5
Thread.sleep(200);

} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("----thread interrupted----");
}
}
}
}

然后我在main里面写了:
if(...)
{
{

sa.pause = false; // sa shi thread
sa.interrupt();
synchronized (sa) { //
sa.notifyAll();
}
}
这样我可以控制暂停的线程重新启动,但是我想写个继续暂停的线程的方法,我就又用了个boolean: resume
然后这样改了下:
if (pause) {
synchronized (this) {
wait();
}
}
if (resume)
{
pause = false;
synchronized (this) { //
this.notifyAll();
}
}
然后我想用resume = true来继续线程,但是不行,请问哪里错了?应该怎么实现这个呢,感觉应该不复杂的,还没搞定,nnd。
...全文
101 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jyf7356759 2008-03-19
  • 打赏
  • 举报
回复
又看了下,感觉和我的不大一样,我想要个方法,可以手动唤醒,就是只要调动这个线程的这个方法,就唤醒暂停的。
继续求助。
jyf7356759 2008-03-18
  • 打赏
  • 举报
回复
哦,谢谢了,我一会再去看看。
allanwxm 2008-03-18
  • 打赏
  • 举报
回复
我也不太懂,你修改的是run()方法吗?是的话,我觉得既然线程已经wait()了,又怎么会去判断resume是否为true呢?我也刚学这块儿,可能说的不对
hmsuccess 2008-03-18
  • 打赏
  • 举报
回复
其实这是个生产者---消费者问题,每当生产者生产一个产品,就要阻塞自己的进程通知消费者来取此产品,
而当消费者消费完这个产品时就阻塞自己的进程,同时唤醒生产者进程
jyf7356759 2008-03-18
  • 打赏
  • 举报
回复
其实我就是想写个方法,调用这个方法的时候,就重新启动暂停的线程。
比如建立一个线程 thread t = new thread();
t.resume(); 就把暂停的这个线程继续。好像不是很复杂吧,大家再帮帮我啊,谢谢。
另外3楼,你的代码我不是很看的懂,不好意思,能解释下么。
girl013579 2008-03-18
  • 打赏
  • 举报
回复
不懂,学习
chensjmail 2008-03-18
  • 打赏
  • 举报
回复
看不明白,把代码都帖出来看看
hmsuccess 2008-03-18
  • 打赏
  • 举报
回复
线程自己当然不能唤醒自己了
参考:

package 线程;

public class Threadtest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CircularBuffer cb=new CircularBuffer(20);

//因为要调用的两个方法put和get是排斥,所以调用时由同一个对象调用,所以
//都是cb,注意注意!!!!!!

Producer pro=new Producer(cb);
Consumer con=new Consumer(cb);
//
Thread a=null;
Thread b=null;

a=new Thread(pro);

b=new Thread(con);
b.start();
a.start();

}

}

import java.io.*;

public class CircularBuffer {

int bufsize;
int[] store;
int numOfEntries=0;
int front=0;
int back=0;

CircularBuffer(int n)
{
bufsize=n;
store=new int[bufsize];

}
synchronized void put(int obj)throws Exception{
if(numOfEntries==bufsize)
{
System.out.println("Producer waiting");

wait();

}
store[back]=obj;
back++;
if(back==bufsize) back=0;
else {numOfEntries++;
System.out.println("putting "+ obj);

notify();
}
}
synchronized int get() throws Exception{
int result=0;
if(numOfEntries==0)
{
System.out.println("Consumer waiting");
wait();
}

else{
result=store[front];
front++;
if(front==bufsize) front=0;



numOfEntries--;
System.out.println("getting "+result);//;
notify();
}
return result;

}
}

public class Producer implements Runnable {

CircularBuffer cbp=null;
Producer(CircularBuffer cb)
{
cbp=cb;
}
public void run(){
for(int i=0;i<=50000;i++)
try{
cbp.put(i);
}
catch (Exception err){
//System.

}

}


}

public class Consumer implements Runnable {

CircularBuffer cbc=null;


Consumer(CircularBuffer cb)
{cbc=cb;}
public void run(){
for(int i=0;i<=50000;i++)
try{
cbc.get();
}
catch (Exception err){

}

}

}



62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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