【JAVA进阶篇教学】第六篇:Java线程中状态

发布于:2024-04-25 ⋅ 阅读:(25) ⋅ 点赞:(0)

博主打算从0-1讲解下java进阶篇教学,今天教学第六篇:Java线程中状态。    

理解并掌握线程的休眠、停止和挂起等操作是多线程编程中的重要内容。下面我将详细说明这些操作,并提供相应的代码案例。

目录

一、线程休眠(Thread Sleep) 

二、线程停止(Thread Stop)

三、线程挂起(Thread Suspend 和 Thread Resume)

四、线程等待(Object Wait)

详细说明


一、线程休眠(Thread Sleep) 

Thread.sleep()方法可以在任何线程中调用,包括主线程和子线程。它可以用于模拟等待、暂停执行或者降低CPU消耗等场景。但是需要注意的是,Thread.sleep()方法会暂停当前线程的执行,因此应该确保不会在关键代码段中调用,以避免影响程序的正常运行。

在Java中,可以使用Thread.sleep()方法来使当前线程休眠一段时间。该方法接受一个毫秒数作为参数,表示线程休眠的时间。

public class ThreadSleepExample {
    public static void main(String[] args) {
        System.out.println("Main thread starts.");

        // 创建一个新线程并启动
        Thread thread = new Thread(new Worker());
        thread.start();

        // 主线程休眠3秒钟
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 主线程结束
        System.out.println("Main thread ends.");
    }

    static class Worker implements Runnable {
        @Override
        public void run() {
            System.out.println("Worker thread starts.");
            // Worker线程休眠2秒钟
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Worker thread ends.");
        }
    }
}

二、线程停止(Thread Stop)

线程停止通常应该由线程本身来控制。可以通过设置一个标志位来控制线程的执行状态,然后在适当的时机检查该标志位并退出线程。这样做可以保证线程的退出过程是安全和可控的。避免直接使用Thread.stop()方法来强制停止线程,因为这可能会导致线程处于不确定的状态,并可能导致数据不一致等问题。

在Java中,通常不推荐直接使用Thread.stop()方法来停止线程,因为这会导致线程处于不确定的状态,并且可能会导致数据不一致等问题。更安全的做法是使用标志位来控制线程的执行状态。

public class ThreadStopExample {
    private static volatile boolean running = true;

    public static void main(String[] args) {
        System.out.println("Main thread starts.");

        // 创建一个新线程并启动
        Thread thread = new Thread(new Worker());
        thread.start();

        // 主线程休眠3秒钟
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 停止Worker线程
        running = false;

        // 主线程结束
        System.out.println("Main thread ends.");
    }

    static class Worker implements Runnable {
        @Override
        public void run() {
            System.out.println("Worker thread starts.");
            while (running) {
                // 执行任务
            }
            System.out.println("Worker thread ends.");
        }
    }
}

三、线程挂起(Thread Suspend 和 Thread Resume)

Thread.suspend()和Thread.resume()方法已经被标记为废弃,不推荐使用。如果确实需要在某些情况下使用这些方法,应该在合适的时机调用,并且谨慎处理线程的挂起和恢复过程。需要注意的是,这些方法可能会导致线程死锁和数据不一致等问题,因此应该尽量避免使用。

在Java中,可以使用Thread.suspend()方法将线程挂起,使用Thread.resume()方法将线程恢复。但是,这两个方法已经被标记为废弃,因为它们可能导致线程死锁和数据不一致等问题,不推荐使用。

public class ThreadSuspendResumeExample {
    public static void main(String[] args) {
        System.out.println("Main thread starts.");

        // 创建一个新线程并启动
        Thread thread = new Thread(new Worker());
        thread.start();

        // 主线程休眠3秒钟
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 挂起Worker线程
        thread.suspend();
        System.out.println("Worker thread suspended.");

        // 主线程休眠3秒钟
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 恢复Worker线程
        thread.resume();
        System.out.println("Worker thread resumed.");

        // 主线程结束
        System.out.println("Main thread ends.");
    }

    static class Worker implements Runnable {
        @Override
        public void run() {
            System.out.println("Worker thread starts.");
            while (true) {
                // 执行任务
            }
        }
    }
}

尽管Thread.suspend()和Thread.resume()方法已经被标记为废弃,但是在某些特定的情况下,仍然可以使用它们。不过需要谨慎使用,以避免可能出现的问题。

四、线程等待(Object Wait)

在Java中,wait()方法是Object类的一个实例方法,用于让当前线程进入等待状态,同时释放对象的锁。当调用wait()方法时,当前线程会被挂起,直到其他线程调用了该对象的notify()或notifyAll()方法来唤醒它,或者等待指定的时间到达。

详细说明

  • 当调用wait()方法时,当前线程会进入对象的等待队列中,并释放对象的锁,允许其他线程访问该对象。
  • 调用wait()方法的线程会一直等待,直到被唤醒(通过notify()或notifyAll()方法),或者等待时间到达(如果提供了超时参数)。
  • 被唤醒的线程会重新进入对象的锁池中,等待获取对象的锁后继续执行。
  • 如果调用wait()方法的线程在等待过程中被中断,会抛出InterruptedException异常。

下面是一个使用wait()和notify()方法的示例,演示了如何实现简单的生产者-消费者模式:

public class ProducerConsumerExample {
    private static final int MAX_CAPACITY = 5;
    private static List<Integer> buffer = new ArrayList<>();

    public static void main(String[] args) {
        Thread producerThread = new Thread(new Producer());
        Thread consumerThread = new Thread(new Consumer());

        producerThread.start();
        consumerThread.start();
    }

    static class Producer implements Runnable {
        @Override
        public void run() {
            while (true) {
                synchronized (buffer) {
                    while (buffer.size() == MAX_CAPACITY) {
                        try {
                            buffer.wait(); // 缓冲区已满,生产者等待
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    int item = (int) (Math.random() * 100);
                    buffer.add(item);
                    System.out.println("Produced: " + item);

                    buffer.notifyAll(); // 唤醒所有消费者
                }
            }
        }
    }

    static class Consumer implements Runnable {
        @Override
        public void run() {
            while (true) {
                synchronized (buffer) {
                    while (buffer.isEmpty()) {
                        try {
                            buffer.wait(); // 缓冲区为空,消费者等待
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    int item = buffer.remove(0);
                    System.out.println("Consumed: " + item);

                    buffer.notifyAll(); // 唤醒所有生产者
                }
            }
        }
    }
}

在上面的示例中,生产者线程和消费者线程共享一个缓冲区buffer,当缓冲区为空时,消费者线程会调用wait()方法进入等待状态,直到生产者线程生产出新的数据并调用notifyAll()方法唤醒它。同样,当缓冲区已满时,生产者线程会调用wait()方法进入等待状态,直到消费者线程消费掉部分数据后调用notifyAll()方法唤醒它。

这样,通过使用wait()和notify()方法,可以实现生产者-消费者模式中的线程协作。