简体   繁体   English

如何对通过实现可运行接口创建的线程进行Sleep()或wait()?

[英]How to Sleep() or wait() a thread created by implementing runnable interface?

I have created class Display and Philosopher by implementing runnable interface. 我通过实现runnable接口创建了DisplayPhilosopher类。 I created many threads in other class of my project. 我在项目的其他类中创建了许多线程。 How to pause those threads by using method wait() (or sleep() ), and then wake them up and continue ( notify() ) when press on the button? 如何通过使用方法wait() (或sleep() )暂停那些线程,然后在按按钮时将其唤醒并继续( notify() )?

Here is my code: 这是我的代码:

public class Display extends JPanel implements Runnable {

    Image bg;
    Image s0, s1, s2, s3, s4;
    Image plate[] = new Image[5];
    Toolkit t;
    Image state1, state2;
    Font font;
    Chopstick chopstick;
    Philosopher philosopher;

    boolean isRunning = false;

    public Display() {
        t = Toolkit.getDefaultToolkit();
        font = new Font("Tahoma", Font.PLAIN, 20);
        bg = t.getImage("image//BG.png");
        s0 = t.getImage("image//ts0.png");
        s1 = t.getImage("image//ts1.png");
        s2 = t.getImage("image//ts2.png");
        s3 = t.getImage("image//ts3.png");
        s4 = t.getImage("image//ts4.png");
        state1 = t.getImage("image//eating.png");
        state2 = t.getImage("image//thinking.png");
        plate[0] = t.getImage("image//p0.png");
        plate[1] = t.getImage("image//p2.png");
        plate[2] = t.getImage("image//p3.png");
        plate[3] = t.getImage("image//p4.png");
        plate[4] = t.getImage("image//p5.png");

        chopstick = new Chopstick();
        philosopher = new Philosopher();

    }

    public void run() {
        while (!isRunning) {
            repaint();

            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean getStop() {
        return isRunning;
    }

    public void setStop(boolean stop) {
        isRunning = stop;
    }

    public void paint(Graphics g) {
        super.paint(g);
        // this.setBackground(Color.white);
        g.drawImage(bg, 0, 0, 800, 600, null);
        drawP(g);
        drawP0(g);
        drawP1(g);
        drawP2(g);
        drawP3(g);
        drawP4(g);
        draw0(g);
        draw1(g);
        draw2(g);
        draw3(g);
        draw4(g);
        drawS0(g);
        drawS1(g);
        drawS2(g);
        drawS3(g);
        drawS4(g);
    }

    public void draw0(Graphics g) {
    }

    public void draw1(Graphics g) {
    }

    public void draw2(Graphics g) {
    }

    public void draw3(Graphics g) {
    }

    public void draw4(Graphics g) {
    }

    public void drawP0(Graphics g) {
    }

    public void drawP1(Graphics g) {
    }

    public void drawP2(Graphics g) {
    }

    public void drawP3(Graphics g) {
    }

    public void drawP4(Graphics g) {
    }

    public void drawP(Graphics g) {
    }

    //
    public void drawS0(Graphics g) {
    }

    public void drawS1(Graphics g) {
    }

    public void drawS2(Graphics g) {
    }

    public void drawS3(Graphics g) {
    }

    public void drawS4(Graphics g) {
    }

    public void drawQuote(Graphics g) {
        if (philosopher.getQuote(0) == 1) {

        }
        if (philosopher.getQuote(0) == 2) {

        }
        if (philosopher.getQuote(0) == 3) {

        }
    }
}


    public class App {

    JButton btPause, btStart;
    JPanel panelButton;
    JFrame f1;
    Chopstick chopstick[];
    Philosopher philosophers[];

    final String TITLE = "Dining Pholosophers Simulator";
    final ImageIcon ICON_START = new ImageIcon("image/run.png");
    final ImageIcon ICON_PAUSE = new ImageIcon("image/play.png");
    final ImageIcon ICON_RESUME = new ImageIcon("image/resume.png");

    boolean isRunning = false;

    public App() {

        f1 = new JFrame();
        //f1.setLayout(null);
        f1.setTitle(TITLE);
        f1.setSize(1120, 680);
        f1.setVisible(true);
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setLocationRelativeTo(null);
        f1.setResizable(false);

        // Khởi tạo 5 cây đũa
        initializePos();
        // Khởi tạo 5 luồng (Triết gia)
        initializePhilosopher();
        Display obj = new Display();
        obj.setBounds(0, 0, 1040, 600);

        // Luồng chính Frame cửa sổ chính
        Thread t1 = new Thread(obj);
        // t1.start();
        // Chạy 5 luồng triết gia
        Thread p1 = new Thread(philosophers[0]);
        // p1.start();
        Thread p2 = new Thread(philosophers[1]);
        // p2.start();
        Thread p3 = new Thread(philosophers[2]);
        // p3.start();
        Thread p4 = new Thread(philosophers[3]);
        // p4.start();
        Thread p5 = new Thread(philosophers[4]);
        // p5.start();
        // f1.add(obj0);
        f1.add(obj);

        //btStart = new JButton("Start...", new ImageIcon("image/pause16.png"));
        btStart = new JButton("Run", ICON_START);
        btPause = new JButton("Pause", ICON_PAUSE);
        btPause.setEnabled(false);

        // Chạy các tiến trình
        btStart.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                t1.start();
                p1.start();
                p2.start();
                p3.start();
                p4.start();
                p5.start();
                isRunning = true;
                btStart.setEnabled(false);
                btPause.setEnabled(true);
            }
        });

        // Đóng băng các tiến trình đang chạy
        btPause.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // Nếu đóng băng
                if (isRunning) {
                    isRunning = false;
                    // Thay đổi trên giao diện
                    btPause.setText("Resume");
                    btPause.setIcon(ICON_RESUME);

                    // I want to call the method to pause those threads here

                } else {
                    isRunning = true;
                    btPause.setText("Pause");
                    btPause.setIcon(ICON_PAUSE);

                    // I want to call the method to continue threads here
                }
            }
        });

        // f1.add(btStart, BorderLayout.SOUTH);
        panelButton = new JPanel();
        panelButton.setLayout(new FlowLayout());
        panelButton.add(btStart);
        panelButton.add(btPause);
        f1.add(panelButton, BorderLayout.SOUTH);
        f1.validate();

    }

    public void initializePos() {
        chopstick = new Chopstick[5];
        chopstick[0] = new Chopstick(0, 270, 330);
        chopstick[1] = new Chopstick(1, 200, 200);
        chopstick[2] = new Chopstick(2, 350, 120);
        chopstick[3] = new Chopstick(3, 500, 210);
        chopstick[4] = new Chopstick(4, 430, 330);
    }

    public void initializePhilosopher() {
        philosophers = new Philosopher[5];
        philosophers[0] = new Philosopher(0, chopstick[0], chopstick[4]);
        philosophers[1] = new Philosopher(1, chopstick[1], chopstick[0]);
        philosophers[2] = new Philosopher(2, chopstick[2], chopstick[1]);
        philosophers[3] = new Philosopher(3, chopstick[3], chopstick[2]);
        philosophers[4] = new Philosopher(4, chopstick[4], chopstick[3]);
    }

    public static void main(String args[]) {
        System.out.println("Simulator is ready, click Start...\n");
        new App();
    }
}

Any help would be greatly appreaciated. 任何帮助将不胜感激。

You could tell the philosophers to wait at an appropriate point in their run() method and then notify them to resume: 您可以告诉哲学家在他们的run()方法中的适当时间等待,然后通知他们恢复:

boolean waitRequested = false;

void requestWait() {
  waitRequested = true;
}

void resume() {
  synchronized(this) {
    notify();
  }
}      

public void run() {      
  while( condition ) {
    try {
      //check if the thread should wait first, if not let it do a full iteration of the loop
      if( waitRequested ) {
        synchronized( this ) {
          wait();

          //resuming here so wait wouldn't be requested anymore
          waitRequested = false;
        }
      }

      //do whatever a philosopher does
    }
    catch( InterruptedException e) {
      //handle exception
    }
  }
}

Then call requestWait() and resume() on the objects. 然后在对象上调用requestWait()resume() Note that you could just call notify() on the objects but you'd then have to surround that with a synchronized block or otherwise you'll most certainly get a IllegalMonitorStateException since the thread that's calling notify() is most likely not the current owner of the monitor. 请注意,您可以只在对象上调用notify() ,但随后必须用一个同步块将其包围,否则肯定会得到一个IllegalMonitorStateException因为正在调用notify()的线程很可能不是当前所有者。显示器的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM