JUC
1.创建多线程
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class MyThread {
public static void main(String[] args) {
new MyThread1().start();
new Thread(new MyThread2()).start();
FutureTask<Integer> futureTask = new FutureTask<>(new MyThread3());
new Thread(futureTask).start();
try {
Integer integer = futureTask.get();
System.out.println(integer);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
class MyThread1 extends Thread{
@Override
public void run() {
System.out.println("MyThread1");
}
}
class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println("MyThread2");
}
}
class MyThread3 implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("MyThread3");
return 100;
}
}
2. Synchronized 和 Lock
简单买票案例
public class Demo01 {
public static void main(String[] args) {
Ticket ticket = new Ticket();
new Thread(() -> {
for (int i = 0; i < 20; i++) {
ticket.sell();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 20; i++) {
ticket.sell();
}
}, "B").start();
}
}
class Ticket {
private static int number = 20;
public synchronized void sell() {
if (number > 0) {
try {
Thread.sleep(20);
System.out.println(Thread.currentThread().getName() + "卖出了第" + (number--) + "张票");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Demo02 {
public static void main(String[] args) {
Ticket2 ticket2 = new Ticket2();
new Thread(() -> {
for (int i = 0; i < 60; i++) {
ticket2.sell();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 60; i++) {
ticket2.sell();
}
}, "B").start();
}
}
class Ticket2 {
private final ReentrantLock lock = new ReentrantLock();
private static int number = 40;
public void sell() {
if (number > 0) {
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + "卖出了第" + (number--) + "张票");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
生产者和消费者
synchronized
public class Demo03 {
public static void main(String[] args) {
Mall mall = new Mall();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
mall.decrement();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
mall.increment();
}
}, "B").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
mall.decrement();
}
}, "C").start();
}
}
class Mall {
private int number = 0;
public synchronized void increment() {
while (number != 0) {
try {
this.wait();
System.out.println(Thread.currentThread().getName() + "==>" + number);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
number++;
}
public synchronized void decrement() {
while (number == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "==>" + number);
this.notifyAll();
number--;
}
public class Demo04 {
public static void main(String[] args) {
Mall2 mall2 = new Mall2();
new Thread(()->{
for (int i = 0; i < 20; i++) {
mall2.increment();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 20; i++) {
mall2.decrement();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 20; i++) {
mall2.increment();
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 20; i++) {
mall2.decrement();
}
},"D").start();
}
}
class Mall2 {
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
private int number = 0;
public void increment(){
lock.lock();
try {
while (number != 0){
condition.await();
}
number++;
System.out.println(Thread.currentThread().getName()+"==>"+number);
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void decrement(){
lock.lock();
try {
while (number == 0){
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+"==>"+number);
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
Lock 可以使用condition实现精准唤醒
Synchronized 与 Lock区别
1.Synchronized 是java的关键字,而Lock是一个接口
2.Synchronized 在退出同步代码块会自动释放同步资源锁,而Lock要自己unlock,否则会死锁
3.竞争少时用synchronized,竞争激烈时用Lock
4.Synchronized 是非公平锁,Lock默认非公平锁
5.Synchronized 不能获取锁的状态,A获得锁后阻塞B会一直等待,Lock可以获取锁的状态trylock,不用一直等待