java并发编程总结--作者--闫明
2019-02-01 17:22:04 0 举报
AI智能生成
java并发编程总结--作者--闫明
作者其他创作
大纲/内容
第一节课:初识多线程及其原理分析
多线程的发展历史
进程
线程
实现了真正意义上的并行执行
线程的应用
什么时候使用多线程
IO/复杂算法
通过并行计算提高性能
如何实现线程
继承Thread
实现Runnable重写run()方法
实现Callable接口重写call()方法
使用线程池Executors.newSingleThreadExecutor()
Thread如何合理的应用到项目中
可以使用责任链模式创建线程完成比较耗时的多步骤操作,
分开执行合理利用多核cpu,提高性能
分开执行合理利用多核cpu,提高性能
并发编程的的基础
线程的状态
new
没有调用start方法
runnable
运行状态
ready就绪
运行
blocked
阻塞
等待阻塞
wait
同步阻塞
synchronized
其他阻塞
sleep、join
waitting
等待
timewaitting
超时等待
terminated
终止
状态之间的转换
线程的启动和终止
start
stop(废弃)
interrupt
volatile Boolean stop=true
Thread.interrupted对设置中断标识的线程进行复位
设置interrupt=false
设置interrupt=false
案例
案例分析:
1、运行案例代码
2、jps查看进程号
3、jstack 进程号查看进程状态
1、运行案例代码
2、jps查看进程号
3、jstack 进程号查看进程状态
Thread state案例
package cn.com.hiveview.mediaapi.module.test.thread;
import java.util.concurrent.TimeUnit;
/**
* Created by Yan on 2019/1/31.
*/
public class ThreadState {
public static void main(String[] args) {
new Thread(() -> {
while (true) {
try {
TimeUnit.SECONDS.sleep(100);
} catch (Exception e) {
}
}
}, "thread timewaitting").start();
new Thread(() -> {
while (true) {
synchronized (ThreadState.class) {
try {
ThreadState.class.wait();
} catch (Exception e) {
}
}
}
}, "waitting ").start();
new Thread (new BlockDemo(),"blocked -0").start();
new Thread (new BlockDemo(),"blocked -1").start();
}
static class BlockDemo extends Thread {
public void run() {
synchronized (BlockDemo.class) {
while (true) {
try {
TimeUnit.SECONDS.sleep(100);
} catch (Exception e) {
}
}
}
}
}
}
package cn.com.hiveview.mediaapi.module.test.thread;
import java.util.concurrent.TimeUnit;
/**
* Created by Yan on 2019/1/31.
*/
public class ThreadState {
public static void main(String[] args) {
new Thread(() -> {
while (true) {
try {
TimeUnit.SECONDS.sleep(100);
} catch (Exception e) {
}
}
}, "thread timewaitting").start();
new Thread(() -> {
while (true) {
synchronized (ThreadState.class) {
try {
ThreadState.class.wait();
} catch (Exception e) {
}
}
}
}, "waitting ").start();
new Thread (new BlockDemo(),"blocked -0").start();
new Thread (new BlockDemo(),"blocked -1").start();
}
static class BlockDemo extends Thread {
public void run() {
synchronized (BlockDemo.class) {
while (true) {
try {
TimeUnit.SECONDS.sleep(100);
} catch (Exception e) {
}
}
}
}
}
}
interrupt案例
package cn.com.hiveview.mediaapi.module.test.thread;
import java.util.concurrent.TimeUnit;
/**
* Created by Yan on 2019/2/1.
*/
public class InterruptDemo {
static int i = 0;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
i++;
}
System.out.println("i=" + i);
}, "interrupt demo");
thread.start();
TimeUnit.SECONDS.sleep(1);
thread.interrupt();
}
}
interrupt会调用os的interrupt。os的interrupt会设置isInterrupted=true
package cn.com.hiveview.mediaapi.module.test.thread;
import java.util.concurrent.TimeUnit;
/**
* Created by Yan on 2019/2/1.
*/
public class InterruptDemo {
static int i = 0;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
i++;
}
System.out.println("i=" + i);
}, "interrupt demo");
thread.start();
TimeUnit.SECONDS.sleep(1);
thread.interrupt();
}
}
interrupt会调用os的interrupt。os的interrupt会设置isInterrupted=true
isInterrupted 案例分析
package cn.com.hiveview.mediaapi.module.test.thread;
import java.util.concurrent.TimeUnit;
/**
* Created by Yan on 2019/2/1.
*/
public class TheadInterruptedDemo {
/*public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true) {
boolean flag = Thread.currentThread().isInterrupted();
if (flag) {
System.out.println("before interrupt flag="+flag);
Thread.currentThread().interrupted();
System.out.println("after interrupted flag="+Thread.currentThread().isInterrupted());
}
}
});
thread.start();
TimeUnit.SECONDS.sleep(1);
thread.interrupt();
}*/
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
while (true){
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("before------ " +thread.isInterrupted());
thread.interrupt();
System.out.println("before " +thread.isInterrupted());
TimeUnit.SECONDS.sleep(1);
System.out.println("after "+thread.isInterrupted());
}
}
before------ false
before true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at cn.com.hiveview.mediaapi.module.test.thread.TheadInterruptedDemo.lambda$main$0(TheadInterruptedDemo.java:28)
at java.lang.Thread.run(Thread.java:748)
after false
package cn.com.hiveview.mediaapi.module.test.thread;
import java.util.concurrent.TimeUnit;
/**
* Created by Yan on 2019/2/1.
*/
public class TheadInterruptedDemo {
/*public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true) {
boolean flag = Thread.currentThread().isInterrupted();
if (flag) {
System.out.println("before interrupt flag="+flag);
Thread.currentThread().interrupted();
System.out.println("after interrupted flag="+Thread.currentThread().isInterrupted());
}
}
});
thread.start();
TimeUnit.SECONDS.sleep(1);
thread.interrupt();
}*/
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
while (true){
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
TimeUnit.SECONDS.sleep(1);
System.out.println("before------ " +thread.isInterrupted());
thread.interrupt();
System.out.println("before " +thread.isInterrupted());
TimeUnit.SECONDS.sleep(1);
System.out.println("after "+thread.isInterrupted());
}
}
before------ false
before true
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at cn.com.hiveview.mediaapi.module.test.thread.TheadInterruptedDemo.lambda$main$0(TheadInterruptedDemo.java:28)
at java.lang.Thread.run(Thread.java:748)
after false
注意:新建线程的时候一定要定义个
有意的线程名称
有意的线程名称
线程安全问题
可见性
原子性
有序性
案例分析
可见性分析
package cn.com.hiveview.mediaapi.module.test.thread;
import java.util.concurrent.TimeUnit;
/**
* Created by Yan on 2019/2/1.
* 有volatile的修饰的时候线程可以中断,实现了线程之间的可见性
*/
public class VisableDemo {
// private static boolean stop=false;
private static volatile boolean stop=false;
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
int i=0;
while (!stop){
i++;
}
});
thread.start();
TimeUnit.SECONDS.sleep(1);
stop=true;
}
}
import java.util.concurrent.TimeUnit;
/**
* Created by Yan on 2019/2/1.
* 有volatile的修饰的时候线程可以中断,实现了线程之间的可见性
*/
public class VisableDemo {
// private static boolean stop=false;
private static volatile boolean stop=false;
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
int i=0;
while (!stop){
i++;
}
});
thread.start();
TimeUnit.SECONDS.sleep(1);
stop=true;
}
}
原子性问题
package cn.com.hiveview.mediaapi.module.test.thread;
/**
* Created by Yan on 2019/2/1.
*/
public class AtomicDemo {
private static int count=0;
private static void inc() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 1000; i++) {
new Thread(AtomicDemo::inc).start();
}
Thread.sleep(4000);
System.out.println("count= "+ count);
}
}
/**
* Created by Yan on 2019/2/1.
*/
public class AtomicDemo {
private static int count=0;
private static void inc() {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
count++;
}
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 1000; i++) {
new Thread(AtomicDemo::inc).start();
}
Thread.sleep(4000);
System.out.println("count= "+ count);
}
}
有序性问题
原因是因为指令重排序
或者编译器重排序产生
或者编译器重排序产生
cpu高速缓存
缓存一致性协议
MESI协议
标志位
M(Modify)(修改)cpu0修改了变量则,
cpu0中对应缓存状态为M,其他cpu中对应缓存为I(失效),
其他cpu进行读取时需要从主内存中加载最新数据
cpu0中对应缓存状态为M,其他cpu中对应缓存为I(失效),
其他cpu进行读取时需要从主内存中加载最新数据
E(Exclusive)(独占)只有一个cpu中有对应的变量,
且这个变量的值与主内存中的值保持一致
且这个变量的值与主内存中的值保持一致
I(Invalid)(无效)
S(Shared)(共享--多个cpu中的缓存是一致的)
嗅探协议
JMM
应用程序层面解决可见性,一致性,有序性问题
分支主题
0 条评论
下一页