2

本质上,我想做的是使用多线程方法启动所有线程,将它们全部暂停,然后将它们全部恢复。我只是在寻找一个简单的解决方案。我不确定我是否必须使用计时器或什么。现在,当我运行它时,线程就像以随机顺序执行(我猜 PC 只是随机选择它想在某个时间运行的线程)。

class ChoppingThread extends Thread
{
    public void run()
    {
        for(int j=40;j!=0;j-=10)
            System.out.println("Chopping vegetables...("+j+" seconds left)");
    }   
 }
class MixingThread extends Thread
{
    public void run()
    {
        for(int k=60;k!=0;k-=10)
            System.out.println("Mixing sauces...("+k+" seconds left)");
    }
}
class TenderizingThread extends Thread
{
    public void run()
    {
        for(int j=50;j!=0;j-=10)
            System.out.println("Tenderizing meat...("+j+" seconds left)");
    }
}
class MultiThreadTasking
{
    public static void main (String [] args)
    {
        ChoppingThread ct = new ChoppingThread();
        MixingThread mt = new MixingThread();
        TenderizingThread tt = new TenderizingThread();

        System.out.println("\nWelcome to the busy kitchen.");
        //putting threads into ready state
        ct.start();
        mt.start();
        tt.start();
    }
}
4

3 回答 3

2

可能还有其他方法可以达到相同的结果,但这是我能想到的最简单的方法(我知道,不是很伤心)......

基本上,这是Runnable具有一些附加管理功能的特殊功能。

这基本上包含一个指示任务状态的状态标志和一个监视器锁

public class ThreadFun {

  public static void main(String[] args) {
    MyTask task = new MyTask();
    Thread thread = new Thread(task);
    thread.start();
    try {
      Thread.sleep(1000);
    } catch (InterruptedException ex) {
    }
    task.pauseTask();
    try {
      Thread.sleep(1000);
    } catch (InterruptedException ex) {
    }
    task.resumeTask();
    try {
      Thread.sleep(1000);
    } catch (InterruptedException ex) {
    }
    task.stopTask();
  }

  public enum TaskState {

    Running,
    Stopped,
    Paused

  }

  public static class MyTask implements Runnable {

    private static final Object PAUSED_LOCK = new Object();

    private volatile TaskState state = TaskState.Running;

    public void pauseTask() {
      if (state == TaskState.Running) {
        System.out.println("Paused...");
        state = TaskState.Paused;
      }
    }

    public void resumeTask() {
      if (state == TaskState.Paused) {
        state = TaskState.Running;
        synchronized (PAUSED_LOCK) {
          PAUSED_LOCK.notifyAll();
        }
        System.out.println("Resumed...");
      }
    }

    public void stopTask() {
      if (state == TaskState.Running || state == TaskState.Paused) {
        state = TaskState.Stopped;
        System.out.println("Stopped...");
      }
    }

    public boolean isStopped() {
      return state == TaskState.Stopped;
    }

    public boolean isPaused() {
      return state == TaskState.Paused;
    }

    protected void doPause() {
      synchronized (PAUSED_LOCK) {
        while (isPaused()) {
          try {
            PAUSED_LOCK.wait();
          } catch (InterruptedException ex) {
          }
        }
      }
    }

    @Override
    public void run() {

      int index = 0;
      while (!isStopped() && index < 1000) {
        try {
          Thread.sleep(25);
        } catch (InterruptedException ex) {
        }
        doPause();
        index++;
        System.out.println(index);
      }
      stopTask(); // Make sure the task is marked as begin stopped ;)

    }

  }
}

主要标准是您需要在适当的时候汇集isStoppeddoPause确保它们按要求开始实施......

于 2013-08-04T03:26:54.590 回答
2

要协调它们,请使用CyclicBarrier

要同时启动它们,请使用CountDownLatch

谷歌上面的两个类有很多例子和解释。

要完全了解正在发生的事情,请阅读Java Concurrency In Practice 一书

于 2013-08-04T03:08:02.267 回答
0

我相信您可以通过使用Object.waitand来实现这一点Thread.interrupt

Object.wait阻塞直到notify被调用。所以

private boolean paused;
private Object waitObject;
...
public void run() {
    for ... {
        if (this.paused) { this.waitObject.wait(); }
    ...

public void pause() { this.paused = true; }
public void resume() { this.paused = false; this.waitObject.notify(); }

然后你可以调用pause来暂停线程。

Thread.interrupt可以帮助停止。

private boolean paused;
...
public void run() {
    for ... {
        // interrupted() is different from interrupt()!
        if (this.iterrupted()) { break; }
    ...

要停止它,您interrupt()将从另一个线程调用。

这是基本的想法,但这里有很多细节需要担心。例如,wait可以抛出一个InterruptedException你需要处理的。此外,wait不保证仅在notify. 它可以随机返回。这是一对教程:

于 2013-08-04T03:37:36.473 回答