JiveJdon Community Forums
在线458人   首页   主题表   培训咨询   标签   查搜   注册    登陆 RSS
首页 » 论坛 » 云架构等伸缩性讨论
???en_US.forumThreadPrev.name??? 上一主题
RSS google yahoo 365Key网摘 CSDN网摘 添加到百度搜藏 POCO网摘 新浪ViVi QQ网摘
???en_US.forumThreadNext.name??? 下一主题
Go 共有 1 回复(1页) 阅读51次
 发表新帖子   回复该主题贴
hti



发表文章:
注册时间: 2005年12月11日
悄悄话
个人博客
在线? 当前离线
我要关注该作者发言
多线程问题 2005年12月11日 19:12 收藏关注本主题 到本帖网址 加入本帖到收藏夹 请用鼠标选择需要回复的文字再点按本回复键 回复该主题
标签
主要是holdResume()和run()这两个方法的实现有点不太明白,不知道如何让线程挂起后,重新启动

望有朋友能指教

class Counter implements Runnable {
// You might use these constants and variables.
private final int IDLE = 0, RUNNING = 1, HOLDING = 2;

private StartResumeStopApplication application;

private int value;

// The following variable is set whenever a new thread is created. It is
// set to null within the run() method whenever the thread executing the
// run() method terminates.
private Thread myThread = null;

Thread t = new Thread(this);

private int state = IDLE;

public Counter(StartResumeStopApplication app) {
value = 0;
application = app;
}

/**
* Upon applying method begin, its counter value is reset, a new thread
* object is created and a thread is started. Subsequent calls to this
* method take no effect unless method terminate has been called.
*/
public synchronized void begin() {
// TODO: Complete the begin() method.
// Set the Counter's state to RUNNING and start a thread.
// Ensure that only a single thread is started!
value = 0;
state = RUNNING;
System.out.println("state: "+state);
t.start();
}

/**
* Upon applying method holdResume, it looks like the counter were
* suspended/resumed. This method has no effect if the counter is in state
* IDLE.
*/
public synchronized void holdResume() {
// TODO: Complete the holdResume() method.
// We have two cases here:
// - we are in the RUNNING state:
// put ourselves into the HOLDING state
// - we are in the HOLDING state:
// put ourselves into the RUNNING state
if (state == 1) {
state = HOLDING;
System.out.println("state: "+state);
}
else
if (state == 2) {
state = RUNNING;
System.out.println("state: "+state);
begin();
}

}

/**
* Upon applying method terminate, the thread is terminated. Subsequent
* calls to this method take no effect unless method begin has been called.
*/
public synchronized void terminate() {
// TODO: Complete the terminate() method.
// Terminate the thread if this Counter object is in the RUNNING state.
// In any case set the Counter object state to IDLE.
if (state == 1)
t.interrupt();

state = IDLE;
System.out.println("state: "+state);
}


private void incr() {
value++;
if (application != null)
application.update(value);
}

public void run() {
// TODO: Complete the thread termination condition.
if (state == 2) {
holdResume();
System.out.println("state: "+state);
}
while (state==1) {
try {
Thread.sleep(100); // 1/10 second
incr();
} catch (InterruptedException e) {
// TODO: Complete the interrupt handler.
// We've been interrupted -- we terminate.
return;

}
}
// Set myThread to null so that control method can determine that
// we really have terminated.
myThread = null;
}
}
banq



发表文章: 11308
注册时间: 2002年08月03日
悄悄话
个人博客
在线? 当前离线
我要关注该作者发言 72人关注
Re: 多线程问题 2005年12月11日 20:46 收藏关注本主题 到本帖网址 加入本帖到收藏夹 请用鼠标选择需要回复的文字再点按本回复键 回复该主题
使用另外一个线程进行监察实现暂停和启动比较好,可参线程编程这本书。
标签
共有 1 回复(1页) Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
正在读取,请等待...
查询本论坛内 回复超过的热门帖子
标题
 
粗体: [b]文本[/b] 斜体: [i]文本[/i] 下划线 [u]文本[/u] 插入网上的图片 [img]http://wwww.xxxx.com/img.ext[/img] 插入代码 [code]程序代码[/code]  插入url链接 [url]http://url[/url] / [url=http://url]URL加下滑线[/url] 上传图片 word文档 Txt等附件
内容
  提交时自动拷贝以上内容到剪贴板 Ctrl+V可取出;提问题前先查询标签列表

使用帮助 手机m.jdon.com RSS add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的软件架构社区 推荐FireFox或Chrome快速浏览本站
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-10 jdon.com

anti spam