一、实现多线程
1.继承Thread类
package bilibili.chapter15;
class Worker extends Thread{
@Override
public void run() {
for(int i=0;i<10;i++) {
System.out.println("Hello!" + i + " "+this.getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class test {
public static void main(String []args){
Worker worker1 = new Worker();
Worker worker2 = new Worker();
worker1.setName("thread-1");
worker2.setName("thread-2");
worker1.start();
worker2.start();
}
}
2.实现Runnable接口
package bilibili.chapter15;
class Worker implements Runnable{
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println("Hello!"+" "+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class test {
public static void main(String []args){
Worker worker = new Worker();
new Thread(worker).start();
new Thread(worker).start();
}
}
3.常用API
package bilibili.chapter15;
class Worker extends Thread{
@Override
public void run() {
for(int i=0;i<10;i++) {
System.out.println("Hello!" + i + " "+this.getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
}
}
public class test {
public static void main(String []args) throws InterruptedException {
Worker worker1 = new Worker();
Worker worker2 = new Worker();
worker1.setName("thread-1");
worker2.setName("thread-2");
//设置为守护线程,当主线程结束后,守护线程也会结束
worker1.setDaemon(true);
worker2.setDaemon(true);
worker1.start();
worker2.start();
//主线程等待其他线程执行完,可以指定时间
//worker1.join(1000);
//中断线程,抛出异常
//worker1.interrupt();
Thread.sleep(5000);
System.out.println("这是主线程");
}
}
二、锁
1.实现方式
package bilibili.chapter15;
import java.util.concurrent.locks.ReentrantLock;
class Lock_Worker extends Thread {
public static int cnt = 0;
private static final ReentrantLock lock = new ReentrantLock();
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
lock.lock();
try {
cnt++;
} finally {
lock.unlock();
}
}
}
}
public class lock {
public static void main(String []args) throws InterruptedException {
Lock_Worker worker1 = new Lock_Worker();
Lock_Worker worker2 = new Lock_Worker();
worker1.start();
worker2.start();
worker1.join();
worker2.join();
System.out.println(Lock_Worker.cnt);
}
}
2.使用Synchronized加载到函数上
package bilibili.chapter15;
import java.util.concurrent.locks.ReentrantLock;
class Lock_Worker implements Runnable{
public static int cnt = 0;
private synchronized void work(){
for (int i=0;i<1000;i++) cnt ++;
}
@Override
public void run() {
work();
}
}
public class lock {
public static void main(String []args) throws InterruptedException {
Lock_Worker worker = new Lock_Worker();
Thread worker1 = new Thread(worker);
Thread worker2 = new Thread(worker);
worker1.start();
worker2.start();
worker1.join();
worker2.join();
System.out.println(Lock_Worker.cnt);
}
}
3.wait()和notify()
package org.yxc;
// Worker类继承自Thread,用于演示线程的等待与通知机制
class Worker extends Thread {
private final Object object;
private final boolean needWait;
// 构造函数
public Worker(Object object, boolean needWait) {
this.object = object;
this.needWait = needWait;
}
@Override
public void run() {
// 同步块,确保多个线程对同一对象进行同步操作
synchronized (object) {
try {
if (needWait) {
// 如果needWait为true,线程等待
object.wait();
System.out.println(this.getName() + ": 被唤醒啦!");
} else {
// 如果needWait为false,唤醒所有等待线程
object.notifyAll();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class Main {
public static void main(String[] args) throws InterruptedException {
// 共享对象,用于线程间通信
Object object = new Object();
// 创建并启动5个需要等待的Worker线程
for (int i = 0; i < 5; i++) {
Worker worker = new Worker(object, true);
worker.setName("thread-" + i);
worker.start();
}
// 创建一个不需要等待的Worker线程,用于唤醒其他线程
Worker worker = new Worker(object, false);
worker.setName("thread-" + 5);
// 主线程睡眠1秒,以确保其他线程都已经开始等待
Thread.sleep(1000);
// 启动唤醒线程
worker.start();
}