JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制
一.静态同步函数的锁是class对象
private static synchronized void show() {
if (tick > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "show:" + tick--);
}
}
- 因为静态方法中,不可以定义this,我们可以分析,静态进内存中,内存中没有本类对象,但是一定有该类的字节码文件对象类名.class,我们可以这样同步
synchronized (MyThread.class)
二.多线成中的单例设计模式
package com.lgl.hellojava;
//公共的 类 类名
public class HelloJJAVA {
public static void main(String[] args) {
/**
* 单例设计模式
*/
}
}
/**
* 饿汉式
*
* @author LGL
*
*/
class Single1 {
private static final Single1 s = new Single1();
private Single1() {
}
public static Single1 getInstance() {
return s;
}
}
/**
* 懒汉式
* @author LGL
*
*/
class Single {
private static Single s = null;
private Single() {
}
public static Single getInstance() {
if (s == null) {
s = new Single();
}
return s;
}
}
public static Single getInstance() {
if (s == null) {
synchronized (Single.class) {
if (s == null) {
s = new Single();
}
}
}
return s;
}
三.多线程的死锁
package com.lgl.hellojava;
//公共的 类 类名
public class HelloJJAVA {
public static void main(String[] args) {
/**
* 需求:简单的卖票程序,多个线程同时卖票
*/
MyThread myThread = new MyThread();
Thread t1 = new Thread(myThread);
Thread t2 = new Thread(myThread);
t1.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myThread.flag = false;
t2.start();
}
}
/**
* 卖票程序
*
* @author LGL
*
*/
class MyThread implements Runnable {
// 票数
private int tick = 100;
Object j = new Object();
boolean flag = true;
@Override
public void run() {
if (flag) {
while (true) {
synchronized (j) {
show();
}
}
} else {
while (true) {
show();
}
}
}
private synchronized void show() {
synchronized (j) {
if (tick > 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "show:" + tick--);
}
}
}
}
四.线程中的通讯
package com.lgl.hellojava;
//公共的 类 类名
public class HelloJJAVA {
public static void main(String[] args) {
/**
* 线程间通讯
*/
}
}
// 资源
class Res {
String name;
String sex;
}
// 输入
class Input implements Runnable {
@Override
public void run() {
}
}
// 输出
class Output implements Runnable {
@Override
public void run() {
}
}
package com.lgl.hellojava;
//公共的 类 类名
public class HelloJJAVA {
public static void main(String[] args) {
/**
* 线程间通讯
*/
Res s = new Res();
Input in = new Input(s);
Output out = new Output(s);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
// 资源
class Res {
String name;
String sex;
}
// 输入
class Input implements Runnable {
private Res s;
public Input(Res s) {
this.s = s;
}
@Override
public void run() {
int x = 0;
while (true) {
if (x == 0) {
s.name = "lgl";
s.sex = "男";
} else if (x == 1) {
s.name = "zhangsan";
s.sex = "女";
}
// 交替
x = (x + 1) % 2;
}
}
}
// 输出
class Output implements Runnable {
private Res s;
public Output(Res s) {
this.s = s;
}
@Override
public void run() {
while (true) {
System.out.println(s.name + "..." + s.sex);
}
}
}
五.线程通讯带来的安全隐患
package com.lgl.hellojava;
//公共的 类 类名
public class HelloJJAVA {
public static void main(String[] args) {
/**
* 线程间通讯
*/
Res s = new Res();
Input in = new Input(s);
Output out = new Output(s);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
// 资源
class Res {
String name;
String sex;
}
// 输入
class Input implements Runnable {
private Res s;
Object o = new Object();
public Input(Res s) {
this.s = s;
}
@Override
public void run() {
int x = 0;
while (true) {
synchronized (Input.class) {
if (x == 0) {
s.name = "lgl";
s.sex = "男";
} else if (x == 1) {
s.name = "zhangsan";
s.sex = "女";
}
// 交替
x = (x + 1) % 2;
}
}
}
}
// 输出
class Output implements Runnable {
private Res s;
public Output(Res s) {
this.s = s;
}
@Override
public void run() {
while (true) {
synchronized (Input.class) {
System.out.println(s.name + "..." + s.sex);
}
}
}
}
六.多线程等待唤醒机制
package com.lgl.hellojava;
//公共的 类 类名
public class HelloJJAVA {
public static void main(String[] args) {
/**
* 线程间通讯
*/
Res s = new Res();
Input in = new Input(s);
Output out = new Output(s);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
// 资源
class Res {
String name;
String sex;
boolean flag = false;
}
// 输入
class Input implements Runnable {
private Res s;
Object o = new Object();
public Input(Res s) {
this.s = s;
}
@Override
public void run() {
int x = 0;
while (true) {
synchronized (Input.class) {
if (s.flag) {
try {
// 等待线程都存放在线程池
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (x == 0) {
s.name = "lgl";
s.sex = "男";
} else if (x == 1) {
s.name = "zhangsan";
s.sex = "女";
}
// 交替
x = (x + 1) % 2;
s.flag = true;
// 通知
notify();
}
}
}
}
// 输出
class Output implements Runnable {
private Res s;
public Output(Res s) {
this.s = s;
}
@Override
public void run() {
while (true) {
synchronized (Input.class) {
if (!s.flag) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println(s.name + "..." + s.sex);
s.flag = false;
notify();
}
}
}
}
}
如果有兴趣,可以加群:555974449