控制线程安全问题
1.前提:
- 当多个线程同时操作同一份资源的时候,才有可能出现线程不安全问题
2.办法:
- 使用同步锁 synchronized ,控制多线程执行时,某一段重要的代码,排队执行,其他代码可以同时执行,又能提高效率,又能控制安全
3.使用方式:
- 1.同步方法 : 在方法上使用synchronized关键字
- 锁的范围太大,效率低,但是简单
public class Web12306_05 implements Runnable{
//票 共享资源
int tickets=100;
public static void main(String[] args) {
Web12306_05 web=new Web12306_05();
Thread th1=new Thread(web,"张三"); //第二个参数为当前线程起名字
Thread th2=new Thread(web,"李四");
Thread th3=new Thread(web,"王五");
th1.start();
th2.start();
th3.start();
}
@Override
public void run() {
//重复的买票
while(true){
//结束循环的条件 没票了就结束 tickets<=0
//A B C
boolean flag=buy();
if(flag){
break;
}
}
}
//买票的流程
//同步成员方法
public synchronized boolean buy(){
if(tickets<=0){
return true;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//买票的人:当前线程的名字 Thread.currentThread().getName()
System.out.println(Thread.currentThread().getName()+"正在购买第"+tickets--+"票");
return false;
}
}
- 同步块
- 同步块 类.calss
- 锁类,相当于把这个类的所有对象,内容都锁住了,如果有些对象不需要锁,我们就只锁某一个对象
public class Web12306_06 implements Runnable{
//票 共享资源
int tickets=100;
public static void main(String[] args) {
Web12306_06 web=new Web12306_06();
Thread th1=new Thread(web,"张三"); //第二个参数为当前线程起名字
Thread th2=new Thread(web,"李四");
Thread th3=new Thread(web,"王五");
th1.start();
th2.start();
th3.start();
}
@Override
public void run() {
//重复的买票
while(true){
//结束循环的条件 没票了就结束 tickets<=0
//A B C
synchronized (Web12306_06.class) { //独一份的,锁之前就存在的,不变的
if(tickets<=0){
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//买票的人:当前线程的名字 Thread.currentThread().getName()
System.out.println(Thread.currentThread().getName()+"正在购买第"+tickets--+"票");
}
}
}
}
- 2.同步块 对象 this
- 锁对象,把这个对象所有的资源全部锁住了,如果指向锁住其中的某一个资源,建议可以锁资源
public class Web12306_07 implements Runnable{
//票 共享资源
int tickets=100;
public static void main(String[] args) {
Web12306_07 web=new Web12306_07();
Thread th1=new Thread(web,"张三"); //第二个参数为当前线程起名字
Thread th2=new Thread(web,"李四");
Thread th3=new Thread(web,"王五");
th1.start();
th2.start();
th3.start();
}
@Override
public void run() {
//重复的买票
while(true){
//结束循环的条件 没票了就结束 tickets<=0
//A B C
synchronized (this) { //当前调用这个成员方法的对象
if(tickets<=0){
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//买票的人:当前线程的名字 Thread.currentThread().getName()
System.out.println(Thread.currentThread().getName()+"正在购买第"+tickets--+"票");
}
}
}
}
- 3.同步块 资源 (成员变量)
- 自定义引用数据类型的对象的地址永远不变
public class Web12306_08 implements Runnable{
//票 共享资源 tickets就指向这个对象,不会改变
Ticket tickets=new Ticket();
public static void main(String[] args) {
Web12306_08 web=new Web12306_08();
Thread th1=new Thread(web,"张三"); //第二个参数为当前线程起名字
Thread th2=new Thread(web,"李四");
Thread th3=new Thread(web,"王五");
th1.start();
th2.start();
th3.start();
}
@Override
public void run() {
//重复的买票
while(true){
//结束循环的条件 没票了就结束 tickets<=0
//A B C
synchronized (tickets) { //tickets 变量,数值
if(tickets.nums<=0){
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
//买票的人:当前线程的名字 Thread.currentThread().getName()
System.out.println(Thread.currentThread().getName()+"正在购买第"+tickets.nums--+"票");
}
}
}
}
//自定义的引用数据类型
class Ticket{
int nums=100;
}