1、多线程的安全
class Ticket implements Runnable
{
private int tick=100;
public void run()
{
while(true)
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...sale..."+tick--);
}
}
}
}
class TicketDemo3
{
public static void main(String[] args)
{
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
Thread t3=new Thread(t);
Thread t4=new Thread(t);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
通过分析,发现,打印出0,-1,-2等错票。
多线程的运行出现了安全问题。
问题的原因:
当多条语句在操作同一个线程共享数据,一个线程对多条语句只执行了一个部分,
还没执行完,另一个线程参与进来执行,导致共享数据的错误。
解决办法:
对多条操作共享数据的语句,只能让一个线程都执行完,在执行过程中,
其他线程不可参与执行。
2、同步代码块
Java对多线程的安全问题提供了专业的解决方式。
就是同步代码块
synchronized(对象)
{
需要被同步的代码
}
对象如同锁,持有锁的线程可以在同步中执行。
没有持有锁的线程及时获取cpu的执行权,也进不去。
比如上火车卫生间----经典。
同步前提:
1,必须要有两个或者两个以上的线程。
2,必须是多个线程使用同一个锁。
必须保证同步中中只能有一个线程执行。
好处:解决了多线程的安全问题。
弊端:多个线程都需要判断锁,较为消耗资源,
3、同步函数
需求:
银行有一个金库,
有两个储户分别存300员,每次存100,存3次。
结果:
4、同步函数的锁是this
函数需要被对象调用,那么函数都有一个所属对象引用,就是this。
所以同步函数使用的锁是this。
通过该程序进行验证。
使用两个线程来买票,
一个线程在同步代码中,
一个线程在同步函数中。
都在执行买票动作
5、静态同步函数的锁是Class对象
当在上代码中的成员变量tick和成员函数show()前面加上static时
还没执行完,另一个线程参与进来执行,导致共享数据的错误。
解决办法:
对多条操作共享数据的语句,只能让一个线程都执行完,在执行过程中,
其他线程不可参与执行。
2、同步代码块
Java对多线程的安全问题提供了专业的解决方式。
就是同步代码块
synchronized(对象)
{
需要被同步的代码
}
对象如同锁,持有锁的线程可以在同步中执行。
没有持有锁的线程及时获取cpu的执行权,也进不去。
比如上火车卫生间----经典。
同步前提:
1,必须要有两个或者两个以上的线程。
2,必须是多个线程使用同一个锁。
必须保证同步中中只能有一个线程执行。
好处:解决了多线程的安全问题。
弊端:多个线程都需要判断锁,较为消耗资源,
3、同步函数
需求:
银行有一个金库,
有两个储户分别存300员,每次存100,存3次。
class Bank
{
private int sum;
//Object obj=new Object();
public synchronized void add(int n) //同步函数
{
//synchronized(obj) //同步代码块
//{
sum+=n;
try{Thread.sleep(10);}catch(Exception e){} //睡眠
System.out.println("sum="+sum);
//}
}
}
class Cus implements Runnable
{
private Bank b=new Bank();
//Object obj=new Object();
public void run()
{
for(int x=0;x<3;x++)
{
//System.out.print(Thread.currentThread().getName());
b.add(100);
}
}
}
class BankDemo
{
public static void main(String[] args)
{
Cus c=new Cus();
Thread t1=new Thread(c);
Thread t2=new Thread(c);
t1.start();
t2.start();
}
}
4、同步函数的锁是this
函数需要被对象调用,那么函数都有一个所属对象引用,就是this。
所以同步函数使用的锁是this。
通过该程序进行验证。
使用两个线程来买票,
一个线程在同步代码中,
一个线程在同步函数中。
都在执行买票动作
class Ticket implements Runnable
{
private int tick=100;
Object obj=new Object();
boolean flag=true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(this)
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...code"+tick--);
}
}
}
}
else
while(true)
show();
}
public synchronized void show()
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+".....show..."+tick--);
}
}
}
class ThisLockDemo
{
public static void main(String[] args)
{
Ticket t=new Ticket();
Thread t1=new Thread(t);
Thread t2=new Thread(t);
//Thread t3=new Thread(t);
//Thread t4=new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag=false;
t2.start();
//t3.start();
//t4.start();
}
}
5、静态同步函数的锁是Class对象
当在上代码中的成员变量tick和成员函数show()前面加上static时
class Ticket implements Runnable
{
private static int tick=100;
Object obj=new Object();
boolean flag=true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(this)
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...code"+tick--);
}
}
}
}
else
while(true)
show();
}
public static synchronized void show()
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+".....show..."+tick--);
}
}
}
发现如果同步函数被静态修饰后,使用的锁是什么?
通过验证,发现不再是this。因为静态方法中也不可以定义this。
静态进内存是,内存中没有本类对象,但是一定有该类对应的字节码文件对象。
类名.class 该对象的类型是class
静态的同步方法,使用的锁是该方法所在类的字节码文件对象。类名.class
这程序不咋用,能看懂就行。
6、单例设计模式-懒汉式
class Single
{
private static Single s=null;
private Single(){}
public static Single getInstance()
{
if(s==null)
s=new Single();
return s;
}
}
多线程
class Single
{
private static Single s=null;
private Single(){}
public static synchronized Single getInstance()
{
if(s==null)
s=new Single();
return s;
}
}
多线程高效
class Single
{
private static Single s=null;
private Single(){}
public static Single getInstance()
{
if(s==null)
{
synchronized(Single.class)
{
if(s==null)
s=new Single();
}
}
return s;
}
}
7、多线程-死锁
class Test implements Runnable
{
private boolean flag;
Test(boolean flag)
{
this.flag=flag;
}
public void run()
{
if(flag)
{
synchronized(MyLock.locka)
{
System.out.println("if locka");
synchronized(MyLock.lockb)
{
System.out.println("if lockb");
}
}
}
else
{
synchronized(MyLock.lockb)
{
System.out.println("else lockb");
synchronized(MyLock.locka)
{
System.out.println("else locka");
}
}
}
}
}
class MyLock
{
static Object locka=new Object();
static Object lockb=new Object();
}
class DeadLockTest
{
public static void main(String[] args)
{
Thread t1=new Thread(new Test(true));
Thread t2=new Thread(new Test(false));
t1.start();
t2.start();
}
}
8、线程通信-示例代码
class Res
{
String name;
String sex;
}
class Input implements Runnable
{
private Res r;
Input(Res r)
{
this.r=r;
}
public void run()
{
int x=0;
while(true)
{
if(x==0)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="丽丽";
r.sex="女女";
}
x=(x+1)%2;
}
}
}
class Output implements Runnable
{
private Res r;
Output(Res r)
{
this.r=r;
}
public void run()
{
while(true)
{
System.out.println(r.name+"....."+r.sex);
}
}
}
class InputOutputDemo
{
public static void main(String[] args)
{
Res r=new Res();
Input in=new Input(r);
Output out=new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
9、线程间通信-解决安全问题
class Input implements Runnable
{
private Res r;
Object obj=new Object();
Input(Res r)
{
this.r=r;
}
public void run()
{
int x=0;
while(true)
{
synchronized(Input.class)
{
if(x==0)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="丽丽";
r.sex="女女";
}
x=(x+1)%2;
}
}
}
}
class Output implements Runnable
{
private Res r;
Object obj=new Object();
Output(Res r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(Input.class)
{
System.out.println(r.name+"....."+r.sex);
}
}
}
}