📅  最后修改于: 2023-12-03 14:56:50.420000             🧑  作者: Mango
Java线程是Java编程语言的核心。线程是一种轻量级进程,使程序可以同时运行多个不同的任务。
Java线程可通过以下方式创建:
class MyThread extends Thread {
public void run() {
System.out.println("MyThread running");
}
}
public class Main {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
class MyRunnable implements Runnable {
public void run() {
System.out.println("MyRunnable running");
}
}
public class Main {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
}
}
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
class MyCallable implements Callable<String> {
public String call() throws Exception {
return "MyCallable running";
}
}
public class Main {
public static void main(String[] args) throws Exception {
Callable<String> c = new MyCallable();
FutureTask<String> ft = new FutureTask<>(c);
Thread t = new Thread(ft);
t.start();
System.out.println(ft.get());
}
}
Java线程可处于以下状态之一:
当多个线程同时访问共享资源时,会产生线程安全问题。为了解决这个问题,Java提供了以下机制:
class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public synchronized void decrement() {
count--;
}
public synchronized int getCount() {
return count;
}
}
public class Main {
public static void main(String[] args) throws Exception {
Counter c = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
c.increment();
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
c.decrement();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(c.getCount());
}
}
import java.util.concurrent.locks.ReentrantLock;
class Counter {
private int count = 0;
private ReentrantLock lock = new ReentrantLock();
public void increment() {
lock.lock();
try {
count++;
} finally {
lock.unlock();
}
}
public void decrement() {
lock.lock();
try {
count--;
} finally {
lock.unlock();
}
}
public int getCount() {
lock.lock();
try {
return count;
} finally {
lock.unlock();
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
Counter c = new Counter();
Thread t1 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
c.increment();
}
});
Thread t2 = new Thread(() -> {
for (int i = 0; i < 1000; i++) {
c.decrement();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println(c.getCount());
}
}
为了避免创建过多线程导致系统资源耗尽,Java提供了线程池。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Main {
public static void main(String[] args) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(2);
pool.execute(() -> {
System.out.println("Task 1 running");
});
pool.execute(() -> {
System.out.println("Task 2 running");
});
pool.shutdown();
}
}
Java提供了以下机制来实现线程间通信:
import java.util.LinkedList;
class ProducerConsumer {
private LinkedList<Integer> list = new LinkedList<>();
private final int LIMIT = 10;
private Object lock = new Object();
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (lock) {
while (list.size() == LIMIT) {
lock.wait();
}
list.add(value++);
lock.notify();
}
}
}
public void consume() throws InterruptedException {
while (true) {
synchronized (lock) {
while (list.size() == 0) {
lock.wait();
}
int value = list.removeFirst();
System.out.println("Value: " + value);
lock.notify();
}
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
ProducerConsumer pc = new ProducerConsumer();
Thread t1 = new Thread(() -> {
try {
pc.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t2 = new Thread(() -> {
try {
pc.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}
import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class ProducerConsumer {
private LinkedList<Integer> list = new LinkedList<>();
private final int LIMIT = 10;
private Lock lock = new ReentrantLock();
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
public void produce() throws InterruptedException {
int value = 0;
while (true) {
lock.lock();
try {
while (list.size() == LIMIT) {
notFull.await();
}
list.add(value++);
notEmpty.signal();
} finally {
lock.unlock();
}
}
}
public void consume() throws InterruptedException {
while (true) {
lock.lock();
try {
while (list.size() == 0) {
notEmpty.await();
}
int value = list.removeFirst();
System.out.println("Value: " + value);
notFull.signal();
} finally {
lock.unlock();
}
}
}
}
public class Main {
public static void main(String[] args) throws Exception {
ProducerConsumer pc = new ProducerConsumer();
Thread t1 = new Thread(() -> {
try {
pc.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t2 = new Thread(() -> {
try {
pc.consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
t2.start();
t1.join();
t2.join();
}
}
Java提供了以下线程安全类: