Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 让线程只是等待_Java_Multithreading - Fatal编程技术网

Java 让线程只是等待

Java 让线程只是等待,java,multithreading,Java,Multithreading,请告诉我如何让线程等待。例如,如果i==0,请等待,当i==1 public class Main { public Main() { } public void method() { Thread thread = new Thread(new Task()); // I want to make wait it when I want // for example wait if i ==

请告诉我如何让线程等待。例如,如果
i==0,请等待,当
i==1

public class Main {

    public Main() { 
    }

    public void method() {

            Thread thread = new Thread(new Task());
            // I want to make wait it when I want
            // for example wait if i == 0 and go again when i = 1
    }

    public static void main(String[] args) {
            new Main();
    }
}

您可以使用

来实现这一点,使用这样的标志不一定是最好的方法,但要回答您的特定问题:您可以使int
变为volatile
。下面是一个可以按原样运行的简单示例,
i
is
volatile
这一事实对于这项工作至关重要

输出为(由于线程交错,每个运行的输出可能不同):

i=1
我在做点什么
我在做点什么
i=0
我在等你
我在等你
i=1
我在做点什么
我在做点什么
我在做点什么
i=0
我在等你
我在等你
打断
我被打断了:再见

这是一个适合的


要避免活动等待,请尝试使用
wait()
notify()
notifyAll()
方法。Wait()可以使线程停止,直到有人在与Wait()相同的对象上调用notify()或notifyAll()。其中一个条件是线程必须拥有将调用wait()、notify()或notifyAll()的对象的监视器

这里有一个例子

import java.util.concurrent.TimeUnit;

public class StartPauseDemo extends Thread {
    volatile int i = 1;

    public void pause() {
        i = 0;
    }

    public synchronized void unPause() {
        i = 1;
        notify();// wake up thread
    }

    @Override
    public void run() {
        while (i==1) {
            // logic of method for example printing time every 200 miliseconds
            System.out.println(System.currentTimeMillis());
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (i==0) {
                synchronized (this) {// thread must possess monitor of object on
                                        // which will be called wait() method,
                                        // in our case current thread object
                    try {
                        wait();// wait until someone calls notify() or notifyAll
                                // on this thred object
                                // (in our case it is done in unPause() method)
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    // test - pausing and unpausing every 1 sec
    public static void main(String[] args) throws InterruptedException {
        StartPauseDemo sp = new StartPauseDemo();
        sp.start();// start thread
        while (true) {
            System.out.println("pausing");
            sp.pause();
            TimeUnit.SECONDS.sleep(1);

            System.out.println("unpausing");
            sp.unPause();
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
输出:

pausing
unpausing
1338726153307
1338726153507
1338726153709
1338726153909
1338726154109
pausing
unpausing
1338726155307
1338726155507
... and so on

这只适用于一个周期。如果他希望能够启动/暂停/启动/暂停等,这将无法正常工作。
import java.util.concurrent.TimeUnit;

public class StartPauseDemo extends Thread {
    volatile int i = 1;

    public void pause() {
        i = 0;
    }

    public synchronized void unPause() {
        i = 1;
        notify();// wake up thread
    }

    @Override
    public void run() {
        while (i==1) {
            // logic of method for example printing time every 200 miliseconds
            System.out.println(System.currentTimeMillis());
            try {
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (i==0) {
                synchronized (this) {// thread must possess monitor of object on
                                        // which will be called wait() method,
                                        // in our case current thread object
                    try {
                        wait();// wait until someone calls notify() or notifyAll
                                // on this thred object
                                // (in our case it is done in unPause() method)
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    // test - pausing and unpausing every 1 sec
    public static void main(String[] args) throws InterruptedException {
        StartPauseDemo sp = new StartPauseDemo();
        sp.start();// start thread
        while (true) {
            System.out.println("pausing");
            sp.pause();
            TimeUnit.SECONDS.sleep(1);

            System.out.println("unpausing");
            sp.unPause();
            TimeUnit.SECONDS.sleep(1);
        }
    }
}
pausing
unpausing
1338726153307
1338726153507
1338726153709
1338726153909
1338726154109
pausing
unpausing
1338726155307
1338726155507
... and so on