Java多线程函数的创建和协作
Java多线程函数的创建和协作
Java是一个以多线程为基础的语言,它支持在程序中同时运行多个不同流程,从而更有效地管理计算机资源。Java多线程的实现主要依赖于线程函数的创建和协作。
线程函数的创建
Java多线程在实现中主要有两种方式:继承Thread和实现Runnable接口。继承Thread类的方式需要重写run()方法,该方法包含了线程执行的逻辑。实现Runnable接口的方式同样需要一个run()方法,并且需要将该接口作为参数传入Thread构造函数。
创建线程的一般步骤如下:
1. 创建一个继承Thread类或实现Runnable接口的类;
2. 重写run()方法,定义线程执行的逻辑;
3. 创建一个Thread对象,将该对象作为参数传入该类的构造函数;
4. 调用Thread对象的start()方法,启动线程。
例如,以下是使用实现Runnable接口的方式创建线程的示例代码:
class MyThread implements Runnable {
public void run() {
System.out.println("MyThread is running.");
}
}
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
}
}
线程函数的协作
Java多线程的协作非常重要,它们可以实现多个线程之间的同步和互斥,保证共享的资源不受到破坏。常用的协作方式有:
1. wait()和notify()方法
wait()和notify()方法是Java多线程协作的基础,它们允许线程等待其他线程的信号来唤醒自己。wait()方法可以使调用该方法的线程等待并释放所占用的资源,直到其他线程调用notify()或notifyAll()方法来唤醒它。notify()方法唤醒等待的线程,使它重新竞争资源。
示例代码:
class MyThread implements Runnable {
public void run() {
synchronized (this) {
System.out.println("Thread1 starts and waits.");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 has been notified and continues running.");
}
}
public void notifyThread() {
synchronized (this) {
notify();
System.out.println("Thread2 notifies Thread1 to wake up.");
}
}
}
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread thread1 = new Thread(myThread);
Thread thread2 = new Thread(() -> myThread.notifyThread());
thread1.start();
thread2.start();
}
}
运行结果:
Thread1 starts and waits. Thread2 notifies Thread1 to wake up. Thread1 has been notified and continues running.
2. join()方法
join()方法允许调用该方法的线程等待其他线程完成后再继续执行。在一个线程中调用另一个线程的join()方法,会使该线程等待被调用线程执行完毕后再执行。
示例代码:
class MyThread implements Runnable {
public void run() {
System.out.println("Thread1 starts.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread1 ends.");
}
}
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
System.out.println("Main thread is running.");
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Main thread ends.");
}
}
运行结果:
Main thread is running. Thread1 starts. Thread1 ends. Main thread ends.
3. interrupt()方法
interrupt()方法可以中断一个正在运行的线程。当线程被中断时,它的中断标志会被设置为true,在线程中可以通过isInterrupted()方法检测该标志。
示例代码:
class MyThread implements Runnable {
public void run() {
System.out.println("Thread1 starts.");
while (!Thread.currentThread().isInterrupted()) {
System.out.println("Thread1 is running.");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread1 ends.");
}
}
public class Main {
public static void main(String[] args) {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread.interrupt();
}
}
运行结果:
Thread1 starts. Thread1 is running. Thread1 is running. Thread1 is running. Thread1 is running. Thread1 is running.
Java多线程的函数创建和协作可以极大提高程序的执行效率和资源利用率,但同时也需要注重线程安全和同步互斥的问题。
