2.多线程-初阶(上)
发布人:shili8
发布时间:2024-11-17 18:20
阅读次数:0
**多线程-初阶(上)**在计算机编程中,多线程是指一个程序可以同时执行多个线程的功能。每个线程都有自己的执行栈和局部变量,线程之间共享同一块内存空间。在 Java 中,多线程使用 Thread 类来实现。
**为什么需要多线程**
在单线程环境下,当一个线程进行长时间的计算或等待时,其他线程将被阻塞。这种情况下,程序的性能和响应速度会大大降低。通过使用多线程,可以让程序同时执行多个任务,从而提高整体的性能和响应速度。
**创建线程**
在 Java 中,创建一个线程需要继承 Thread 类或实现 Runnable 接口。
### 继承 Thread 类
javapublic class MyThread extends Thread {
@Override public void run() {
System.out.println("Hello, World!");
}
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start(); // 启动线程 }
}
### 实现 Runnable 接口
javapublic class MyRunnable implements Runnable {
@Override public void run() {
System.out.println("Hello, World!");
}
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start(); // 启动线程 }
}
**线程的状态**
一个线程可以处于以下几种状态:
* **NEW**:新创建的线程,尚未启动。
* **RUNNABLE**:线程正在执行或准备执行。
* **BLOCKED**:线程被阻塞等待某个资源。
* **WAITING**:线程正在等待另一个线程完成特定操作。
* **TIMED_WAITING**:线程正在等待另一个线程完成特定操作,超时时间为指定的毫秒数。
* **TERMINATED**:线程已经结束。
javapublic class ThreadStatus {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
System.out.println("NEW");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("RUNNABLE");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("BLOCKED");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("WAITING");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("TIMED_WAITING");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("TERMINATED");
});
thread.start();
while (!thread.isAlive()) {}
System.out.println(thread.getState());
}
}
**线程的优先级**
每个线程都有一个优先级,范围从1 到10。优先级越高,线程执行的频率越快。
javapublic class ThreadPriority {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i =0; i < 10000000; i++) {}
});
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
while (!thread.isAlive()) {}
System.out.println(thread.getPriority());
}
}
**线程的同步**
多个线程共享同一块内存空间时,需要使用同步机制来避免数据竞争。
javapublic class ThreadSync {
private static int count =0;
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
for (int i =0; i < 10000000; i++) {
synchronized (ThreadSync.class) {
count++;
}
}
});
Thread thread2 = new Thread(() -> {
for (int i =0; i < 10000000; i++) {
synchronized (ThreadSync.class) {
count--;
}
}
});
thread1.start();
thread2.start();
while (!thread1.isAlive() || !thread2.isAlive()) {}
System.out.println(count);
}
}
**线程的通信**
多个线程之间需要进行通信来完成特定任务。
javapublic class ThreadComm {
private static int count =0;
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
for (int i =0; i < 10000000; i++) {
synchronized (ThreadComm.class) {
if (count ==0) {
System.out.println("Thread1: count is0");
}
count++;
}
}
});
Thread thread2 = new Thread(() -> {
for (int i =0; i < 10000000; i++) {
synchronized (ThreadComm.class) {
if (count ==10000000) {
System.out.println("Thread2: count is10000000");
}
count--;
}
}
});
thread1.start();
thread2.start();
while (!thread1.isAlive() || !thread2.isAlive()) {}
System.out.println(count);
}
}
**线程的join**
多个线程之间需要进行 join 操作来等待特定线程完成。
javapublic class ThreadJoin {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i =0; i < 10000000; i++) {}
});
thread.start();
thread.join(); // 等待线程完成 System.out.println("Thread has finished");
}
}
**线程的interrupt**
多个线程之间需要进行 interrupt 操作来中断特定线程。
javapublic class ThreadInterrupt {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i =0; i < 10000000; i++) {}
});
thread.start();
thread.interrupt(); // 中断线程 System.out.println("Thread has been interrupted");
}
}
**线程的sleep**
多个线程之间需要进行 sleep 操作来暂停特定线程。
javapublic class ThreadSleep {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i =0; i < 10000000; i++) {}
});
thread.start();
try {
Thread.sleep(1000); // 暂停线程 } catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Thread has slept");
}
}
**线程的yield**
多个线程之间需要进行 yield 操作来让出 CPU 时间片。
javapublic class ThreadYield {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i =0; i < 10000000; i++) {}
});
thread.start();
Thread.yield(); // 让出 CPU 时间片 System.out.println("Thread has yielded");
}
}
**线程的run**
多个线程之间需要进行 run 操作来执行特定任务。
javapublic class ThreadRun {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i =0; i < 10000000; i++) {}
});
thread.start();
thread.run(); // 执行线程任务 System.out.println("Thread has run");
}
}
**线程的start**
多个线程之间需要进行 start 操作来启动特定线程。
javapublic class ThreadStart {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i =0; i < 10000000; i++) {}
});
thread.start(); // 启动线程 System.out.println("Thread has started");
}
}
**线程的stop**
多个线程之间需要进行 stop 操作来停止特定线程。
javapublic class ThreadStop {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
for (int i =0; i < 10000000; i++) {}
});
thread.start();
thread.stop(); // 停止线程 System.out.println("Thread has stopped");
}
}
**线程的destroy**
多个线程之间需要进行 destroy 操作来销毁特定线程。
java

