今日内容
一、多线程线程安全
1.什么是多线程安全
2.演示多线程线程安全及同步方法
package com.itmayiedu;
class ThreadDemo01 implements Runnable {
// 同时多个窗口共享100
private int count = 100;
public void run() {
while (count > 0) {
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
sale();
}
}
public synchronized void sale() {
if (count > 0) {
System.out.println(Thread.currentThread().getName() + ",出售" + (100 - count + 1) + "张票");
count--;
}
}
}
public class Test0001 {
public static void main(String[] args) {
ThreadDemo01 threadDemo01 = new ThreadDemo01();
Thread t1 = new Thread(threadDemo01, "窗口1");
Thread t2 = new Thread(threadDemo01, "窗口2");
t1.start();
t2.start();
}
}
3.同步代码块解决线程安全
package com.itmayiedu;
class ThreadDemo01 implements Runnable {
// 同时多个窗口共享100
private int count = 100;// 存放方法区中
private Object oj = new Object();
public void run() {
while (count > 0) {
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
sale();
}
}
public void sale() {
synchronized(oj) {
if (count > 0) {
System.out.println(Thread.currentThread().getName() + ",出售" + (100 - count + 1) + "张票");
count--;
}
}
}
}
public class Test0001 {
public static void main(String[] args) {
ThreadDemo01 threadDemo01 = new ThreadDemo01();
Thread t1 = new Thread(threadDemo01, "窗口1");
Thread t2 = new Thread(threadDemo01, "窗口2");
t1.start();
t2.start();
}
}
package com.itmayiedu;
class ThreadDemo01 implements Runnable {
// 同时多个窗口共享100
private static int count = 100;// 存放方法区中
private static Object oj = new Object();
public void run() {
while (count > 0) {
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
sale();
}
}
public void sale() {
synchronized(oj) {
if (count > 0) {
System.out.println(Thread.currentThread().getName() + ",出售" + (100 - count + 1) + "张票");
count--;
}
}
}
}
public class Test0001 {
public static void main(String[] args) {
ThreadDemo01 threadDemo01 = new ThreadDemo01();
ThreadDemo01 threadDemo02 = new ThreadDemo02();
Thread t1 = new Thread(threadDemo01, "窗口1");
Thread t2 = new Thread(threadDemo02, "窗口2");
t1.start();
t2.start();
}
}
4.静态同步代码块
package com.itmayiedu;
class ThreadDemo01 implements Runnable {
// 同时多个窗口共享100
private volatile static int count = 100;// 存放方法区中
private static Object oj = new Object();
public boolean flag = true;
public void run() {
if (flag) {
while (count > 0) {
// synchronized (this.getClass()) {
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
if (count > 0) {
System.out.println(Thread.currentThread().getName() + ",出售" + (100 - count + 1) + "张票");
count--;
}
}
// }
} else {
while (count > 0) {
sale();
}
}
}
// public synchronized void sale() {
// if (count > 0) {
// System.out.println(Thread.currentThread().getName() + ",出售" + (100 -
// count + 1) + "张票");
// count--;
// }
// }
public static void sale() {
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
if (count > 0) {
System.out.println(Thread.currentThread().getName() + ",出售" + (100 - count + 1) + "张票");
count--;
}
}
}
public class Test0001 {
public static void main(String[] args) throws InterruptedException {
ThreadDemo01 threadDemo01 = new ThreadDemo01();
Thread t1 = new Thread(threadDemo01, "窗口1");
Thread t2 = new Thread(threadDemo01, "窗口2");
t1.start();
Thread.sleep(40);
threadDemo01.flag = false;
t2.start();
}
}
5.多线程死锁产生的原因
package com.itmayiedu;
class ThreadDemo02 implements Runnable {
// 同时多个窗口共享100
private volatile static int count = 100;// 存放方法区中
private static Object oj = new Object();
public boolean flag = true;
public void run() {
if (flag) {
while (count > 0) {
// synchronized (oj) {
// try {
// Thread.sleep(10);
// } catch (Exception e) {
// // TODO: handle exception
// }
sale();
// }
}
} else {
while (count > 0) {
sale();
}
}
}
public synchronized void sale() {
synchronized (oj) {
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
if (count > 0) {
System.out.println(Thread.currentThread().getName() + ",出售" + (100 - count + 1) + "张票");
count--;
}
}
}
}
public class Test0002 {
public static void main(String[] args) throws InterruptedException {
ThreadDemo02 threadDemo02 = new ThreadDemo02();
Thread t1 = new Thread(threadDemo02, "窗口1");
Thread t2 = new Thread(threadDemo02, "窗口2");
t1.start();
Thread.sleep(40);
threadDemo02.flag = false;
t2.start();
}
}
6.threadlocal
package com.itmayiedu;
class Res {
public Integer count = 0;
public Integer getNumber() {
return ++count;
}
}
public class Test0003 extends Thread {
private Res res;
public Test0003(Res res) {
this.res = res;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + "," + res.getNumber());
}
}
public static void main(String[] args) {
Res res = new Res();
Test0003 t1 = new Test0003(res);
Test0003 t2 = new Test0003(res);
t1.start();
t2.start();
}
}
package com.itmayiedu;
class Res {
public Integer count = 0;
public Integer getNumber() {
return ++count;
}
}
public class Test0003 extends Thread {
private Res res;
public Test0003(Res res) {
this.res = res;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + "," + res.getNumber());
}
}
public static void main(String[] args) {
Res res1 = new Res();
Res res2 = new Res();
Test0003 t1 = new Test0003(res1);
Test0003 t2 = new Test0003(res2);
t1.start();
t2.start();
}
}
package com.itmayiedu;
class Res {
public static ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
protected Integer initialValue() {
return 0;
};
};
public Integer getNumber() {
int count = threadLocal.get() + 1;
threadLocal.set(count);
return count;
}
}
public class Test0003 extends Thread {
private Res res;
public Test0003(Res res) {
this.res = res;
}
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println(Thread.currentThread().getName() + "," + res.getNumber());
}
}
public static void main(String[] args) {
Res res = new Res();
Test0003 t1 = new Test0003(res);
Test0003 t2 = new Test0003(res);
t1.start();
t2.start();
}
}
二、Java内存模型
1.volatile关键字
package com.itmayiedu;
class ThreadDemo004 extends Thread {
public volatile boolean flag = true;
@Override
public void run() {
System.out.println("线程开始...");
while (flag) {
}
System.out.println("线程結束...");
}
public void setRuning(boolean flag){
this.flag=flag;
}
}
public class Test0004 {
public static void main(String[] args) throws InterruptedException {
ThreadDemo004 threadDemo004 = new ThreadDemo004();
threadDemo004.start();
Thread.sleep(3000);
threadDemo004.setRuning(false);
System.out.println("flag已經改為false");
Thread.sleep(1000);
System.out.println("flag:"+threadDemo004.flag);
}
}
2.synchronized与Volatile
Volatile与Synchronized区别
(1)从而我们可以看出volatile虽然具有可见性但是并不能保证原子性。
(2)性能方面,synchronized关键字是防止多个线程同时执行一段代码,就会影响程序执行效率,而volatile关键字在某些情况下性能要优于synchronized。
但是要注意volatile关键字是无法替代synchronized关键字的,因为volatile关键字无法保证操作的原子性。
3.什么是重排序
package com.itmayiedu;
class ReorderExample {
volatile int a = 0;
volatile boolean flag = false;
// 写入线程
public void writer() {
flag = true; // 2
a = 1; // 1
}
// 1.1行代码和2行代码没有任何依赖关系
// 读取的线程
public void reader() {
if (flag) { // 3
int i = a * a; // 4 0
}
}
}