这门课中的【并发】一词涵盖了在 Java 平台上的
以及 Java 并发工具、并发问题以及解决方案,同时我也会讲解一些其它领域的并发
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test1")
public class Test1 {
public static void main(String[] args) {
Thread t = new Thread("thread") {
@Override
public void run() {
log.debug("thrad is running");
}
};
t.setName("threadT");
t.start();
}
}
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test2")
public class Test2 {
public static void main(String[] args) {
//代码执行体
Runnable runnable = new Runnable() {
@Override
public void run() {
log.debug("thrad is running");
}
};
//创建线程
Thread t = new Thread(runnable, "thread");
t.setName("threadT");
t.start();
}
}
FutureTask 能够接收 Callable 类型的参数,用来处理有返回结果的情况
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
@Slf4j(topic = "c.Test1")
public class Test3{
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask task = new FutureTask(new Callable() {
@Override
public Integer call() throws Exception {
log.debug("thrad is running");
Thread.sleep(1000);
return 100;
}
});
Thread t = new Thread(task, "threadT");
t.start();
log.debug("{}", task.get());
}
}
java -Djava.rmi.server.hostname=`ip地址` -Dcom.sun.management.jmxremote -
Dcom.sun.management.jmxremote.port=`连接端口` -Dcom.sun.management.jmxremote.ssl=是否安全连接 -
Dcom.sun.management.jmxremote.authenticate=是否认证 java类
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test7")
public class Test7 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug("enter sleep...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
//休眠线程被打断这时 sleep 方法会抛出 InterruptedException
log.debug("wake up...");
e.printStackTrace();
}
}
};
t1.start();
//主线程休眠,t1线程加入
Thread.sleep(1000);
log.debug("interrupt...");
//其它线程可以使用 interrupt 方法打断正在睡眠的线程
t1.interrupt();
}
}
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test9")
public class Test9 {
public static void main(String[] args) {
Runnable task1 = () -> {
int count = 0;
for (; ; ) {
System.out.println("---->1 " + count++);
}
};
Runnable task2 = () -> {
int count = 0;
for (; ; ) {
//调用 yield 会让当前线程从 Running 进入 Runnable 就绪状态,然后调度执行其它线程
Thread.yield();
System.out.println(" ---->2 " + count++);
}
};
Thread t1 = new Thread(task1, "t1");
Thread t2 = new Thread(task2, "t2");
t1.start();
t2.start();
}
}
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test9")
public class Test9 {
public static void main(String[] args) {
Runnable task1 = () -> {
int count = 0;
for (;;) {
System.out.println("---->1 " + count++);
}
};
Runnable task2 = () -> {
int count = 0;
for (;;) {
System.out.println(" ---->2 " + count++);
}
};
Thread t1 = new Thread(task1, "t1");
Thread t2 = new Thread(task2, "t2");
//调度器优先调度该线程,但它仅仅是一个提示,调度器可以忽略它
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
下面的代码执行,打印 r 是什么?
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
import static cn.itcast.n2.util.Sleeper.sleep;
@Slf4j(topic = "c.Test10")
public class Test10 {
static int r = 0;
public static void main(String[] args) throws InterruptedException {
test1();
}
private static void test1() throws InterruptedException {
log.debug("开始");
Thread t1 = new Thread(() -> {
log.debug("开始");
sleep(1);
log.debug("结束");
r = 10;
},"t1");
t1.start();
//t1线程加入主线程,主线程等待t1线程结束在结束
//t1.join();
//打印结果为0,因为主线程代码不会等待其他线程先执行
log.debug("结果为:{}", r);
log.debug("结束");
}
}
应用之同步
打断 sleep,wait,join 的线程
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test11")
public class Test11 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
log.debug("sleep...");
try {
Thread.sleep(5000); // wait, join一样
} catch (InterruptedException e) {
//被阻塞的线程被打断会报InterruptedException异常
e.printStackTrace();
}
},"t1");
t1.start();
Thread.sleep(1000);
log.debug("interrupt");
//打断标记,让线程停止
t1.interrupt();
//打断 sleep 的线程, 会清空打断状态,isInterrupted()的值为false
log.debug("打断标记:{}", t1.isInterrupted());
}
}
输出:
打断正常运行的线程
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
/**
* intercupt方法的使用
*/
@Slf4j(topic = "c.Test12")
public class Test12 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
while (true) {
//获取当前线程的打断标记
boolean interrupted = Thread.currentThread().isInterrupted();
//未阻塞线程,手动处理打断标记
if (interrupted) {
log.debug("被intercupt方法打断");
break;
}
}
}, "t");
t.start();
//让主线程等待一会儿,不然t线程未执行调用intercupt方法会报错
Thread.sleep(1000);
log.debug("intercupt");
//调用该方法时,会把调用线程的该方法返回值设置为true,但是不会直接终止线程,要终止线程则需要我们手动编码实现
t.interrupt();
}
}
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.locks.LockSupport;
import static cn.itcast.n2.util.Sleeper.sleep;
@Slf4j(topic = "c.Test14")
public class Test14 {
private static void test4() {
Thread t1 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
log.debug("park...");
//让线程t1阻塞
LockSupport.park();
log.debug("打断状态:{}", Thread.interrupted());
}
});
t1.start();
sleep(1);
t1.interrupt();
}
private static void test3() throws InterruptedException {
Thread t1 = new Thread(() -> {
log.debug("park...");
//让线程t1阻塞
LockSupport.park();
log.debug("unpark...");
log.debug("打断状态:{}", Thread.currentThread().isInterrupted());
}, "t1");
t1.start();
sleep(1);
t1.interrupt();
}
public static void main(String[] args) throws InterruptedException {
test4();
}
}
提示:可以使用 Thread.interrupted() 清除打断状态
package cn.itcast.test;
import lombok.extern.slf4j.Slf4j;
@Slf4j(topic = "c.Test15")
public class Test15 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
while (true) {
if (Thread.currentThread().isInterrupted()) {
break;
}
}
log.debug("结束");
}, "t1");
//t1设置为守护线程,当主线程结束,守护线程一起结束
t1.setDaemon(true);
t1.start();
Thread.sleep(1000);
log.debug("结束");
}
}
javaAPI的六种状态代码演示
package cn.itcast.n3;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
@Slf4j(topic = "c.TestState")
public class TestState {
public static void main(String[] args) throws IOException {
Thread t1 = new Thread("t1") {
@Override
public void run() {
log.debug("running...");
}
};
Thread t2 = new Thread("t2") {
@Override
public void run() {
//while-true一直处于运行状态
while (true) { // runnable
}
}
};
t2.start();
Thread t3 = new Thread("t3") {
@Override
public void run() {
log.debug("running...");
}
};
t3.start();
Thread t4 = new Thread("t4") {
@Override
public void run() {
synchronized (TestState.class) {
try {
//有时间约束等待
Thread.sleep(1000000); // timed_waiting
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t4.start();
Thread t5 = new Thread("t5") {
@Override
public void run() {
try {
//无时间约束等待
t2.join(); // waiting
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t5.start();
Thread t6 = new Thread("t6") {
@Override
public void run() {
//t4线程会先拿到TestState.class这把锁,但是没有释放,所以是blocked阻塞
synchronized (TestState.class) { // blocked
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t6.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("t1 state {}", t1.getState());
log.debug("t2 state {}", t2.getState());
log.debug("t3 state {}", t3.getState());
log.debug("t4 state {}", t4.getState());
log.debug("t5 state {}", t5.getState());
log.debug("t6 state {}", t6.getState());
System.in.read();
}
}
package cn.itcast.test;
import cn.itcast.n2.util.Sleeper;
import lombok.extern.slf4j.Slf4j;
import static cn.itcast.n2.util.Sleeper.sleep;
@Slf4j(topic = "c.Test16")
public class Test16 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
log.debug("洗水壶");
sleep(1);
log.debug("烧开水");
sleep(5);
},"老王");
Thread t2 = new Thread(() -> {
log.debug("洗茶壶");
sleep(1);
log.debug("洗茶杯");
sleep(2);
log.debug("拿茶叶");
sleep(1);
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("泡茶");
},"小王");
t1.start();
t2.start();
}
}
Java 的体现
而 Java 的内存模型如下,完成静态变量的自增,自减需要在主存和工作内存中进行数据交换:
如果是单线程以上 8 行代码是顺序执行(不会交错)没有问题:
出现正数的情况
static int counter = 0;
static void increment()
// 临界区
{
counter++;
}
static void decrement()
// 临界区
{
counter--;
}
synchronized 实际是用对象锁保证了临界区内代码的原子性,临界区内的代码对外是不可分割的,不会被线程切换所打断。
所谓的“线程八锁”
MarkWord存放monitor的地址指针
Owner:哪个线程是这把锁的持有者
EntryList:阻塞队列,进入BLOCKED状态
普通对象头
数组对象头
Obj对象在加锁时:会清除在MarkWord里记录的hashcode、age、biased_lock,然后记录Monitor的指针地址(占30位),加锁状态由01(Normal)改为10(Heavyweight Locked)
Monitor 结构如下
synchronized 原理
static final Object lock = new Object();
static int counter = 0;
public static void main(String[] args) {
synchronized (lock) {
counter++;
}
}
synchronized 原理进阶
4. 偏向锁
偏向状态
撤销 - 调用对象 hashCode
批量重偏向
5. 锁消除
API 介绍