java多线程——同步与死锁

本节目标:
了解线程同步的作用
了解同步代码块及同步方法的作用
了解死锁的产生

1.同步

同步是指多个操作在同一时间段内只有一个线程进行,其他线程要等待此线程完成之后才可以执行。

public class MyThread2 implements Runnable {
private int ticket = 5; // 表示有5张票
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (this.ticket > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("卖票:ticket=" + this.ticket--);
}
}
}
public static void main(String[] args) {
MyThread2 mt1 = new MyThread2(); // 实例化对象
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt1);
Thread t3 = new Thread(mt1);
t1.start();
t2.start();
t3.start();
}
}
运行结果:
卖票:ticket=5
卖票:ticket=4
卖票:ticket=5
卖票:ticket=3
卖票:ticket=2
卖票:ticket=1
卖票:ticket=0

卖票:ticket=-1


程序中加入延迟操作,模拟现实中网络延迟情况。在第一步和第二步之间,因为有延迟,第一步在没有对
票数减一操作之前,其他线程就已经对票数减少了,因而出现了票数为负数的情况。
问题解决:使用同步操作


同步代码块:
代码块分为四种:
普通代码块:直接定义在方法内
构造块:直接定义在类中,优先于构造方法执行,重复调用
静态块:static关键字声明,优先于构造块执行,仅执行一次
同步代码块:使用synchronized关键字声明

格式:
synchronized(同步对象){
同步代码;
}

public class MyThread2 implements Runnable {
private int ticket = 5; // 表示有5张票


@Override
public void run() {
for (int i = 0; i < 100; i++) {
synchronized (this) {
if (this.ticket > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("卖票:ticket=" + this.ticket--);
}
}
}
}


public static void main(String[] args) {
MyThread2 mt1 = new MyThread2(); // 实例化对象
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt1);
Thread t3 = new Thread(mt1);
t1.start();
t2.start();
t3.start();
}
}
同步方法:
synchronized 方法返回值类型 方法名(参数列表){}

public class MyThread2 implements Runnable {
private int ticket = 5; // 表示有5张票
@Override
public void run() {
for (int i = 0; i < 100; i++) {
this.sale();
}
}
synchronized void sale(){ //同步方法
synchronized (this) {
if (this.ticket > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("卖票:ticket=" + this.ticket--);
}
}
}
public static void main(String[] args) {
MyThread2 mt1 = new MyThread2(); // 实例化对象
Thread t1 = new Thread(mt1);
Thread t2 = new Thread(mt1);
Thread t3 = new Thread(mt1);
t1.start();
t2.start();
t3.start();
}
}

2.死锁

1)资源共享时需要同步操作
2)程序中过多的同步会产生死锁

如上图所示,两个线程处于“胶着状态”,产生死锁。


java中最常用的方法定义格式总结:

访问权限{public|default|protected|private} [final][static][synchronized] 返回值类型|void 方法名(参数列表) throws Exception{

[return [返回值|返回调用处]]

}

案例:生产者与消费者





class Info {
private String name;
private String content ;
boolean flag = false; //设置标志位,默认可以消费
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public  void setContent(String content) {
this.content = content;
}

public synchronized void set(String name, String content){
if (!flag) {
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.content = content;
flag = false; //已生产,改变标志位,可以消费
super.notify();
}
public synchronized void get(){
if (flag) {
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("消费者:" + this.name + "-" + this.content);
flag = true;  //已消费,改变标志位,可以生产
super.notify();
}
};
class Producer implements Runnable{
private Info info = null;
public Producer(Info info) {
this.info = info;
}
@Override
public void run() {
boolean flag = false;
for (int i = 0; i < 50; i++) {
if (flag) {
this.info.set("Devin", "java");
System.out.println("生产者:" + this.info.getName() +"-" + this.info.getContent());
flag = false;
}else{
this.info.set("ym", "bd");
System.out.println("生产者:" + this.info.getName() +"-" + this.info.getContent());
flag = true;
}
}
}
};
class Consumer implements Runnable{
private Info info = null;
public Consumer(Info info) {
this.info = info;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
this.info.get();
}
}
};
public class MyThread4 {
public static void main(String[] args) {
Info info = new Info();
Producer pro = new Producer(info);
Consumer con = new Consumer(info);
new Thread(pro).start(); 
new Thread(con).start(); 
}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值