解决方案 »

  1.   


    public class Test {

    public static void main(String[] args) {

    final Thread t1 = new Thread() {

    public void run() {

    int i = 0;

    while(!Thread.interrupted()) {

    System.out.println(++ i);

    try {
    Thread.sleep(500L);
    } catch (InterruptedException e) {
    break;
    }

    }

    }

    };

    Thread t2 = new Thread() {

    public void run() {

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

    t1.interrupt(); //中断t1

    }

    };

    t1.start();
    t2.start();

    }}
      

  2.   

    import java.util.concurrent.Semaphore;
    public class Test {

    public static void main(String[] args) {

    final Semaphore available = new Semaphore(1);

    final Thread t1 = new Thread() {

    public void run() {

    int i = 0;

    while(true) {

    try {
    available.acquire();
    System.out.println(++ i);
    Thread.sleep(500L);
    available.release();
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }

    }

    }

    };

    Thread t2 = new Thread() {

    public void run() {

    try {
    Thread.sleep(1000L);

    available.acquire();
    System.out.println("暂停");

    Thread.sleep(5000L);
    available.release();

    System.out.println("开始");

    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    }

    };

    t1.start();
    t2.start();

    }}
      

  3.   

    public class Test {

    public static void main(String[] args) {

    TaskA t1 = new TaskA();
    TaskB t2 = new TaskB(t1);

    t1.start();
    t2.start();

    }}class TaskA extends Thread {

    int i;
    boolean stoped; public void run() {

    while(true) {

    if(stoped) {
    synchronized(this) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    doTask();

    }

    }

    void doTask() {

    if(i < 100) {
    System.out.println(++ i);
    try {
    Thread.sleep(500L);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }

    }

    void zanting() {
    stoped = true;
    }

    synchronized void jixu() {
    stoped = false;
    notifyAll();
    }

    }class TaskB extends Thread {

    TaskA taskA;

    TaskB(TaskA taskA) {
    this.taskA = taskA;
    }

    public void run() {

    try {

    Thread.sleep(1000L);

    taskA.zanting();
    System.out.println("暂停");

    for (char i = 'a'; i < 'f'; i++) {
    System.out.println(i);
    Thread.sleep(500L);
    }

    taskA.jixu();
    System.out.println("开始");

    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    }

    }
      

  4.   

    我就是想找那种线程1可以立即暂停线程2的方法,就算线程2正在执行当前while的一个循环的中途也就在中途暂停,让线程1执行完业务,唤醒线程2,线程2才可继续执行循环。
    也不知道到底能否实现。
      

  5.   

    Object的wait()和notifyAll()方法,使用这两个方法让线程暂停,并且还能恢复,只需要封装一下:public abstract class MyThread extends Thread {  
      
        private boolean suspend = false;  
      
        private String control = ""; // 只是需要一个对象而已,这个对象没有实际意义  
      
        public void setSuspend(boolean suspend) {  
            if (!suspend) {  
                synchronized (control) {  
                    control.notifyAll();  
                }  
            }  
            this.suspend = suspend;  
        }  
      
        public boolean isSuspend() {  
            return this.suspend;  
        }  
      
        public void run() {  
            while (true) {  
                synchronized (control) {  
                    if (suspend) {  
                        try {  
                            control.wait();  
                        } catch (InterruptedException e) {  
                            e.printStackTrace();  
                        }  
                    }  
                }  
                this.runPersonelLogic();  
            }  
        }  
      
        protected abstract void runPersonelLogic();  
          
        public static void main(String[] args) throws Exception {  
            MyThread myThread = new MyThread() {  
                protected void runPersonelLogic() {  
                    System.out.println("myThead is running");  
                }  
            };  
            myThread.start();  
            Thread.sleep(3000);  
            myThread.setSuspend(true);  
            System.out.println("myThread has stopped");  
            Thread.sleep(3000);  
            myThread.setSuspend(false);  
        }  
    }  
      

  6.   

    这个可以实现,要多写一些代码,
    被控制线程每执行一句代码后,判断一下标记,如果为标记false让线程执行wait()
    控制方只要控制标记就能让另一个线程停止,执行notifyAll()并让标记为true就能让另一个线程继续