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

Java线程操作相关函数的使用方法详解

发布时间:2023-06-24 20:56:36

Java中线程操作是非常常见的,也是Java语言的一大特色。Java中线程操作函数丰富,常用的有sleep、join、yield、wait和notify等。下面我们来详细介绍Java中线程操作相关函数的使用方法。

1. sleep()方法

sleep()方法的作用是让当前线程暂停执行,让出CPU给其他线程。该方法有一个参数,表示暂停时间的毫秒数。当线程进入sleep()状态后,它不会释放对象锁。

sleep()方法的使用方法如下:

public static void sleep(long millis) throws InterruptedException

例如:

try {

    System.out.println("Sleeping...");

    Thread.sleep(1000);

    System.out.println("Awake!");

} catch(InterruptedException e) {

    System.out.println("Interrupted!");

}

执行结果为:

Sleeping...

Awake!

2. join()方法

join()方法的作用是让当前线程等待其他线程完成后再继续执行。该方法有一个参数,表示等待时间的毫秒数。当线程进入join()状态后,它会释放对象锁。

join()方法的使用方法如下:

public static void join(long millis) throws InterruptedException

例如:

Thread t1 = new Thread(new Runnable() {

    public void run() {

        System.out.println("t1 running");

    }

});

Thread t2 = new Thread(new Runnable() {

    public void run() {

        try {

            t1.join();

            System.out.println("t2 running");

        } catch (InterruptedException e) {

            System.out.println("Interrupted!");

        }

    }

});

t1.start();

t2.start();

执行结果为:

t1 running

t2 running

3. yield()方法

yield()方法的作用是让当前线程放弃当前CPU时间片,让出CPU给其他线程。该方法没有参数,当线程进入yield()状态后,它不会释放对象锁。

yield()方法的使用方法如下:

public static native void yield()

例如:

Thread t1 = new Thread(new Runnable() {

    public void run() {

        for(int i=0; i<5; i++) {

            System.out.println("t1 running");

            Thread.yield();

        }

    }

});

Thread t2 = new Thread(new Runnable() {

    public void run() {

        for(int i=0; i<5; i++) {

            System.out.println("t2 running");

        }

    }

});

t1.start();

t2.start();

执行结果为:

t1 running

t2 running

t1 running

t2 running

t1 running

t2 running

t1 running

t2 running

t1 running

t2 running

4. wait()方法

wait()方法的作用是让当前线程进入等待状态,等待其他线程通知唤醒。该方法必须在同步的代码块或方法中使用,当线程进入wait()状态后,它会释放对象锁。

wait()方法的使用方法如下:

public final void wait() throws InterruptedException

例如:

final Object o = new Object();

Thread t1 = new Thread(new Runnable() {

    public void run() {

        synchronized(o) {

            try {

                o.wait();

                System.out.println("t1 running");

            } catch (InterruptedException e) {

                System.out.println("Interrupted!");

            }

        }

    }

});

Thread t2 = new Thread(new Runnable() {

    public void run() {

        synchronized(o) {

            System.out.println("t2 running");

            o.notifyAll();

        }

    }

});

t1.start();

t2.start();

执行结果为:

t2 running

t1 running

5. notify()方法

notify()方法的作用是唤醒一个等待中的线程,让它继续执行。该方法必须在同步的代码块或方法中使用,当线程调用notify()方法时,它并不会立即释放对象锁。

notify()方法的使用方法如下:

public final void notify()

例如:

final Object o = new Object();

Thread t1 = new Thread(new Runnable() {

    public void run() {

        synchronized(o) {

            try {

                o.wait();

                System.out.println("t1 running");

            } catch (InterruptedException e) {

                System.out.println("Interrupted!");

            }

        }

    }

});

Thread t2 = new Thread(new Runnable() {

    public void run() {

        synchronized(o) {

            System.out.println("t2 running");

            o.notify();

        }

    }

});

t1.start();

t2.start();

执行结果为:

t2 running

t1 running

综上所述,Java中线程操作函数使用方法需要注意函数的含义和使用条件,合理使用可以提高代码效率和执行效果,避免一些潜在的问题。