进程:
线程:
并发:
并行:
基本原理:
基本原理:
继承Thread类:
public class ThreadTest { public static void main(String[] args){ new TestThread().start(); } } class TestThread extends Thread{ @Override public void run(){ System.out.println("Test Thread"); } }
实现Runnable接口:
public class ThreadTest { public static void main(String[] args){ new Thread(new TestRunnable()).start(); } } class TestRunnable implements Runnable{ @Override public void run() { System.out.println("Test Runnable"); } }
实现Callable类:
public class ThreadTest { public static void main(String[] args){ FutureTask<Integer> task = new FutureTask(new TestCallable()); new Thread(task).start(); try { System.out.println(task.get());//get方法会得到call执行完之后的值 } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } class TestCallable implements Callable<Integer> { @Override public Integer call() throws Exception { int num = 0; while(num < 5){ num++; } return num; } }
sleep():
wait():
public class ThreadTest { protected static volatile Object lock = "lock"; public static void main(String[] args){ for(int i=0;i<5;i++){ new Thread(() -> { synchronized(lock){ System.out.println(Thread.currentThread().getName() + ":等待开始当前时间戳:" + System.currentTimeMillis()); try { // lock.wait(1000);//让当前线程进入等待池 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } }
notify(),notifyAll():
public class ThreadTest { protected static volatile Object lock = "lock"; public static void main(String[] args){ new Thread(new TestRunnable(lock)).start(); try { Thread.sleep(1000); System.out.println("sleep 1000ms"); } catch (InterruptedException e) { e.printStackTrace(); } new TestThread(lock).start(); } } class TestRunnable implements Runnable{ private Object lock; public TestRunnable(Object lock){ this.lock = lock; } @Override public void run() { try { synchronized (lock){ System.out.println("begin wait:" + System.currentTimeMillis()); System.out.println("Release lock........"); lock.wait();//让当前线程进入等待池 System.out.println("end wait:" + System.currentTimeMillis()); } } catch (InterruptedException e) { e.printStackTrace(); } } } class TestThread extends Thread{ private Object lock; public TestThread(Object lock){ this.lock = lock; } @Override public void run() { synchronized (lock){ System.out.println("begin notify:" + System.currentTimeMillis()); System.out.println("do something........"); lock.notify();//从等待池中随机唤醒一个wait线程进入锁池竞争锁 // lock.notifyAll();//从等待池中随机唤醒所有wait线程进入锁池竞争锁 System.out.println("end notify:" + System.currentTimeMillis()); } } }
start():
run():
主要体现在下面三方面:
死锁:
public class ThreadTest { //共享资源A private static Object A = new Object(); //共享资源B private static Object B = new Object(); public static void main(String[] args){ new Thread(() -> { synchronized (A){ System.out.println(Thread.currentThread().getName() + ":得到资源---A"); try { Thread.sleep(1000);//此处休眠是为了让其他线程获得执行机会 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":去获取资源---B"); synchronized (B){ System.out.println(Thread.currentThread().getName() + ":得到资源---B"); } } },"Thread-01").start(); new Thread(() -> { synchronized (B){ System.out.println(Thread.currentThread().getName() + ":得到资源---B"); try { Thread.sleep(1000);//此处休眠是为了让其他线程获得执行机会 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":去获取资源---A"); synchronized (A){ System.out.println(Thread.currentThread().getName() + ":得到资源---A"); } } },"Thread-02").start(); } }
死锁四个必要条件即如何避免死锁:
public class ThreadTest {
//共享资源A
private static Object A = new Object();
//共享资源B
private static Object B = new Object();
public static void main(String[] args){
new Thread(() -> {
synchronized (A){
System.out.println(Thread.currentThread().getName() + ":得到资源---A");
try {
Thread.sleep(1000);//此处休眠是为了让其他线程获得执行机会
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":去获取资源---B");
synchronized (B){
System.out.println(Thread.currentThread().getName() + ":得到资源---B");
}
}
},"Thread-01").start();
new Thread(() -> {
//线程2去获取A时被阻塞
synchronized (A){
System.out.println(Thread.currentThread().getName() + ":得到资源---A");
try {
Thread.sleep(1000);//此处休眠是为了让其他线程获得执行机会
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ":去获取资源---B");
synchronized (B){
System.out.println(Thread.currentThread().getName() + ":得到资源---B");
}
}
},"Thread-02").start();
}
}
(以上所有内容皆为个人笔记,如有错误之处还望指正。)
原文:https://www.cnblogs.com/xihuantingfeng/p/11628820.html