多线程的运行有安全问题。
必须保证同步中只能有一个线程在运行。
如何找到同步代码:
1,明确哪些代码是多线程运行代码。
2,明确共享数据。
3,明确多线程运行代码中哪些语句是操作共享数据的。
弊端:多个线程需要判断锁,较为消耗资源。
synchronized:它包括两种用法:synchronized 方法和 synchronized 块。
1.synchronized方法:通过在方法声明中加入 synchronized关键字来声明 synchronized 方法。如:
public synchronized void mathod(int a);
synchronized 方法的缺陷:若将一个大的方法声明为synchronized 将会大大影响效率,典型地,若将线程类的方法 run() 声明为
synchronized ,由于在线程的整个生命期内它一直在运行,因此将导致它对本类任何 synchronized 方法的调用都永远不会成功。当然我们可以通过将访问类成员变量的代码放到专门的方法中,将其声明为 synchronized ,并在主方法中调用来解决这一问题,但是 Java 为我们提供了更好的解决办法,那就是 synchronized 块。
class Test { public static void main(String[] args) { Demo d=new Demo(); Thread d1=new Thread(d); Thread d2=new Thread(d); d1.start(); d2.start(); } } class Demo implements Runnable { int tick=10; public synchronized void run()//synchronized方法 { while(tick>0) { { try{Thread.sleep(10);}catch(Exception e){} System.out.println(Thread.currentThread()+"......run......."+tick--); } } } }2. synchronized 块:通过 synchronized关键字来声明synchronized 块。语法如下:
class Test
{
public static void main(String[] args)
{
Demo d=new Demo();
Thread d1=new Thread(d);
Thread d2=new Thread(d);
d1.start();
d2.start();
}
}
class Demo implements Runnable
{
int tick=10;
Object o=new Object();
public void run()
{
while(tick>0)
{
synchronized(o)//synchronized块,也可以是synchronized(this)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread()+"......run......."+tick--);
}
}
}
}
class Test
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag=false;
t2.start();
}
}
class Ticket implements Runnable
{
private static int tick = 100;
Object obj = new Object();
boolean flag = true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(Ticket.class)
{
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()//Ticket.class
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....show.... : "+ tick--);
}
}
}
死锁:同步中嵌套同步。
package Test; //死锁 class Demo10 implements Runnable { private boolean flag; Demo10(boolean flag) { this.flag = flag; } public void run() { if(flag) { while(true) { synchronized(MyLock.locka) { System.out.println(Thread.currentThread().getName()+"...if locka "); synchronized(MyLock.lockb) { System.out.println(Thread.currentThread().getName()+"..if lockb"); } } } } else { while(true) { synchronized(MyLock.lockb) { System.out.println(Thread.currentThread().getName()+"..else lockb"); synchronized(MyLock.locka) { System.out.println(Thread.currentThread().getName()+".....else locka"); } } } } } } class MyLock { static Object locka = new Object(); static Object lockb = new Object(); } class Test10 { public static void main(String[] args) { Thread t1 = new Thread(new Demo10(true)); Thread t2 = new Thread(new Demo10(false)); t1.start(); t2.start(); } }