多執行緒問題
主要是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;
}
}
望有朋友能指教
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;
}
}
相關文章
- java多執行緒執行問題Java執行緒
- Java多執行緒面試高配問題---多執行緒(3)🧵Java執行緒面試
- 多執行緒問題解釋執行緒
- 多執行緒相關問題執行緒
- Hibernate 多執行緒問題!執行緒
- 多執行緒鎖的問題執行緒
- Java多執行緒中執行緒安全與鎖問題Java執行緒
- 多執行緒程式設計,處理多執行緒的併發問題(執行緒池)執行緒程式設計
- java多執行緒問題 多核cpu遇上java多執行緒,求解釋Java執行緒
- 多執行緒之8鎖問題執行緒
- 05.java多執行緒問題Java執行緒
- 【java 多執行緒】多執行緒併發同步問題及解決方法Java執行緒
- 多執行緒-多執行緒常見的面試題執行緒面試題
- 多執行緒,你覺得你安全了?(執行緒安全問題)執行緒
- 多執行緒引起的效能問題分析執行緒
- HashMap多執行緒併發問題分析HashMap執行緒
- 多執行緒併發常見問題執行緒
- Java 多執行緒面試問題彙總Java執行緒面試
- seam中使用多執行緒的問題執行緒
- 小小問題―關於java多執行緒Java執行緒
- 一個多執行緒的PushbackInputStream問題執行緒
- 多執行緒面試題執行緒面試題
- 多執行緒下的程式同步(執行緒同步問題總結篇)執行緒
- 執行緒問題執行緒
- 5分鐘搞懂多執行緒安全問題執行緒
- 多執行緒併發安全問題詳解執行緒
- 如何解決多執行緒併發問題執行緒
- 40 個 Java 多執行緒問題總結Java執行緒
- BATJ都愛問的多執行緒面試題BAT執行緒面試題
- 多執行緒的安全性問題(三)執行緒
- Java多執行緒和併發問題集Java執行緒
- 多執行緒-死鎖問題概述和使用執行緒
- 多執行緒 HashMap 死迴圈 問題解析執行緒HashMap
- objective-c 多執行緒注意的問題Object執行緒
- 40個Java多執行緒問題總結Java執行緒
- 請教一個多執行緒的問題執行緒
- j2me多執行緒遇到問題執行緒
- 多執行緒-執行緒安全問題的產生原因分析以及同步程式碼塊的方式解決執行緒安全問題執行緒