多執行緒問題
主要是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執行緒面試
- 多執行緒之8鎖問題執行緒
- 05.java多執行緒問題Java執行緒
- 多執行緒,你覺得你安全了?(執行緒安全問題)執行緒
- 多執行緒引起的效能問題分析執行緒
- HashMap多執行緒併發問題分析HashMap執行緒
- Spring中多執行緒的使用及問題Spring執行緒
- 5分鐘搞懂多執行緒安全問題執行緒
- 多執行緒的安全性問題(三)執行緒
- 如何解決多執行緒併發問題執行緒
- 多執行緒併發安全問題詳解執行緒
- 40 個 Java 多執行緒問題總結Java執行緒
- Java多執行緒和併發問題集Java執行緒
- iOS多執行緒全套:執行緒生命週期,多執行緒的四種解決方案,執行緒安全問題,GCD的使用,NSOperation的使用iOS執行緒GC
- 多執行緒面試題執行緒面試題
- 多執行緒和多執行緒同步執行緒
- 【多執行緒】常見問題簡單總結執行緒
- WebMagic多執行緒導致註解失效問題Web執行緒
- Faiss使用多執行緒出現的效能問題AI執行緒
- 多執行緒 日誌 和截圖的問題執行緒
- 多執行緒的安全問題及解決方案執行緒
- BATJ都愛問的多執行緒面試題BAT執行緒面試題
- 執行緒與多執行緒執行緒
- 多執行緒【執行緒池】執行緒
- 多執行緒--執行緒管理執行緒
- Python執行緒專題10:queue、多執行緒按順序執行Python執行緒
- 03 執行緒安全問題執行緒
- SimpleDateFormat 執行緒安全問題ORM執行緒
- 多執行緒面試題1執行緒面試題
- Linux多執行緒面試題Linux執行緒面試題
- Java多執行緒開發|volatile與偽共享問題Java執行緒
- 多執行緒併發同步問題及解決方案執行緒
- 有個關於多執行緒的識別問題執行緒
- Java中解決多執行緒資料安全問題Java執行緒
- Java多執行緒-執行緒中止Java執行緒
- 多執行緒之初識執行緒執行緒