欢迎访问宙启技术站
智能推送

Java多线程函数实例详解

发布时间:2023-08-04 19:44:25

Java语言中,多线程编程是一种常见的编程方式。使用多线程可以实现并发执行任务,提高程序的执行效率。本文将介绍Java多线程函数的实例,并详细解析每个函数的作用。

1. sleep()函数:让当前线程休眠一段时间。在指定的时间内,当前线程不会执行任务,其他线程可以继续执行。sleep()函数的参数是一个时间段,单位是毫秒。

示例代码:

public class SleepThreadExample {

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

        Thread thread1 = new Thread(new MyRunnable());
        Thread thread2 = new Thread(new MyRunnable());

        thread1.start();
        thread2.start();

        System.out.println("End");
    }

    static class MyRunnable implements Runnable {
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " is running");
                Thread.sleep(1000);     // 线程休眠一秒
                System.out.println(Thread.currentThread().getName() + " is done");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

输出结果:

Start
Thread-0 is running
Thread-1 is running
End
Thread-1 is done
Thread-0 is done

2. join()函数:等待线程终止。在主线程中调用线程的join()方法,主线程将等待被调用线程执行完毕后再继续执行。

示例代码:

public class JoinThreadExample {

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new MyRunnable());
        Thread thread2 = new Thread(new MyRunnable());

        thread1.start();
        thread2.start();

        // 主线程等待thread1和thread2执行完毕
        thread1.join();
        thread2.join();

        System.out.println("All threads are done");
    }

    static class MyRunnable implements Runnable {
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " is running");
                Thread.sleep(1000);     // 线程休眠一秒
                System.out.println(Thread.currentThread().getName() + " is done");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

输出结果:

Thread-1 is running
Thread-0 is running
Thread-1 is done
Thread-0 is done
All threads are done

3. wait()和notify()函数:wait()方法使当前线程进入等待状态,直到其他线程调用notify()方法唤醒它。

示例代码:

public class WaitNotifyExample {

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(new MyRunnable());
        Thread thread2 = new Thread(new MyRunnable());

        thread1.start();
        thread2.start();

        // 主线程等待thread1和thread2执行完毕
        thread1.join();
        thread2.join();

        System.out.println("All threads are done");
    }

    static class MyRunnable implements Runnable {
        public void run() {
            try {
                System.out.println(Thread.currentThread().getName() + " is running");
                synchronized (this) {
                    wait();     // 线程等待
                }
                System.out.println(Thread.currentThread().getName() + " is done");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

输出结果:

Thread-0 is running
Thread-1 is running

4. yield()函数:让出CPU的执行时间,使得其他具有相同优先级的线程有机会执行。

示例代码:

public class YieldThreadExample {

    public static void main(String[] args) {
        Thread thread1 = new MyThread();
        Thread thread2 = new MyThread();

        thread1.start();
        thread2.start();

        System.out.println("All threads are done");
    }

    static class MyThread extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                System.out.println(Thread.currentThread().getName() + " is running");
                Thread.yield();     // 让出CPU的执行时间
            }
            System.out.println(Thread.currentThread().getName() + " is done");
        }
    }
}

输出结果:

Thread-0 is running
Thread-1 is running
Thread-0 is running
Thread-1 is running
Thread-0 is running
Thread-1 is running
Thread-0 is running
Thread-1 is running
Thread-1 is running
Thread-1 is done
Thread-0 is done
All threads are done

以上就是Java多线程函数的实例和详细解析。通过使用不同的多线程函数,可以更好地控制线程的执行顺序、优先级和时间切片,实现多线程编程的需求。