1、线程安全
线程安全的三种解决方法:同步代码块,同步方法和lock锁。
线程安全是多线程编程时的计算机程序代码中的一个概念。在拥有共享数据的多条线程并行执行的程序中,线程安全的代码会通过同步机制保证各个线程都可以正常且正确的执行,不会出现数据污染等意外情况。
1、同步代码块
详细代码:
publicclass同步代码块 { publicstaticvoidmain(String[] args) { TicketWindow1task=newTicketWindow1(); newThread(task,"窗口一").start(); newThread(task,"窗口二").start(); newThread(task,"窗口三").start(); } } classTicketWindow1implementsRunnable{ privateinttickets=10; Objectlock=newObject(); publicvoidrun() { while(true){ synchronized (lock) { try { Thread.sleep(10); } catch (InterruptedExceptione) { // TODO Auto-generated catch blocke.printStackTrace(); } if(tickets>0){ System.out.println(Thread.currentThread().getName()+"--卖出的票"+tickets--); } } } } }
2、同步方法
详细代码:
publicclass同步方法 { publicstaticvoidmain(String[] args) { TicketWindow2task=newTicketWindow2(); newThread(task,"窗口一").start(); newThread(task,"窗口二").start(); newThread(task,"窗口三").start(); } } classTicketWindow2implementsRunnable{ privateinttickets=10; publicvoidrun() { // TODO Auto-generated method stubwhile(true){ SendTask(); } } privatesynchronizedvoidSendTask() { // TODO Auto-generated method stubtry { Thread.sleep(10); } catch (InterruptedExceptione) { // TODO Auto-generated catch blocke.printStackTrace(); } if(tickets>0){ System.out.println(Thread.currentThread().getName()+"--卖出的票"+tickets--); }else{ System.exit(0); } } }
3、lock锁
详细代码:
importjava.util.concurrent.locks.Lock; importjava.util.concurrent.locks.ReentrantLock; publicclasslock锁 { publicstaticvoidmain(String[] args) { TicketWindow3task=newTicketWindow3(); newThread(task,"窗口一").start(); newThread(task,"窗口二").start(); newThread(task,"窗口三").start(); } } classTicketWindow3implementsRunnable{ privateinttickets=10; Lockl=newReentrantLock(); publicvoidrun() { while(true){ l.lock(); try { Thread.sleep(10); } catch (InterruptedExceptione) { // TODO Auto-generated catch blocke.printStackTrace(); } if(tickets>0){ System.out.println(Thread.currentThread().getName()+"--卖出的票"+tickets--); } l.unlock(); } } }
2、死锁
死锁是指两个或两个以上的进程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进程称为死锁进程。
publicclass线程死锁implementsRunnable { staticObjectchopsticks=newObject(); staticObjectknifeAndFork=newObject(); privatebooleanflag; 线程死锁(booleanflag){ this.flag=flag; } publicvoidrun() { // TODO Auto-generated method stubif(flag){ while(true){ synchronized (chopsticks) { System.out.println(Thread.currentThread().getName()+"---if---chopsticks"); synchronized (knifeAndFork) { System.out.println(Thread.currentThread().getName()+"---if---knifeAndFork"); } } } }else{ while(true){ synchronized (knifeAndFork) { System.out.println(Thread.currentThread().getName()+"---else---knifeAndFork"); synchronized (chopsticks) { System.out.println(Thread.currentThread().getName()+"---else---chopsticks"); } } } } } }
publicclassExample { publicstaticvoidmain(String[] args) { 线程死锁d1=new线程死锁(true); 线程死锁d2=new线程死锁(false); newThread(d1,"Chinese").start(); newThread(d2,"American").start(); } }
3、线程通信
ITC,是Inter-Thread Communication的缩写,意思是线程间通信。
publicclassMyThread { publicstaticObjectobject=newObject(); publicstaticvoidmain(String[] args) { Thread1t1=newThread1(); Thread2t2=newThread2(); t1.start(); t2.start(); } staticclassThread1extendsThread{ publicvoidrun() { synchronized (object) { System.out.println("线程"+Thread.currentThread().getName()+"获取到了锁"); try{ System.out.println("线程"+Thread.currentThread().getName()+"阻塞并释放锁。。。"); object.wait(); }catch(InterruptedExceptione){ } System.out.println("线程"+Thread.currentThread().getName()+"执行完成。。。。。。"); } } staticclassThread2extendsThread{ publicvoidrun() { synchronized (object) { System.out.println("线程"+Thread.currentThread().getName()+"获取到了锁"); object.notify(); System.out.println("线程"+Thread.currentThread().getName()+"唤醒了正在wait的线程"); } System.out.println("线程"+Thread.currentThread().getName()+"执行完成。。。。。。"); } } } }