欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

线程同步与死锁

程序员文章站 2022-04-17 14:05:54
...

多线程对同一资源的访问,如果处理不当,会造成数据的错误;
线程同步:依靠,当某一线程执行操作时,其余线程外面等待;

  • 使用synchronized关键字,在同步代码块中的代码只允许一个线程执行;synchronized(同步对象)
class ticket implements Runnable
{
    private int ticketNumber=10;
    @Override
    public void run() {
        while(true)
        {
            synchronized (this)
            {
                if(ticketNumber>0)
                {
                    try{
                        Thread.sleep(100);
                    }catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"卖票,ticket="+this.ticketNumber--);
                }
                else
                {
                    System.out.println("******票已卖光*******");
                    break;
                }
            }
        }
    }
}
public class buyTicket {
    public static void main(String[] args)throws Exception {
        ticket mt=new ticket();
        new Thread(mt,"票贩子A").start();
        new Thread(mt,"票贩子B").start();
        new Thread(mt,"票贩子C").start();
    }
}

加入同步执行后,程序执行性能会降低;

  • 利用同步方法解决
class ticket implements Runnable
{
    private int ticketNumber=10;
    public synchronized boolean sale() {
                if(ticketNumber>0)
                {
                    try{
                        Thread.sleep(1000);
                    }catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"卖票,ticket="+this.ticketNumber--);
                    return true;
                }
                else
                {
                    System.out.println("******票已卖光*******");
                    return false;
                }
    }

    @Override
    public void run() {
        while(this.sale())
        {
            ;
        }
    }
}
public class buyTicket {
    public static void main(String[] args)throws Exception {
        ticket mt=new ticket();
        new Thread(mt,"票贩子A").start();
        new Thread(mt,"票贩子B").start();
        new Thread(mt,"票贩子C").start();
    }
}

死锁:线程是若干个线程彼此互相等待;

class Jian
{
    public synchronized void say(XiaoQiang xq)
    {
        System.out.println("阿健说:此路是我开,要想过此路,留下10块钱");
        xq.get();
    }
    public synchronized  void get()
    {
        System.out.println("阿健说:得到了10块钱,可以吃饭了");
    }
}
class XiaoQiang
{
    public synchronized void say(Jian JJ)
    {
        System.out.println("小强说:我先过,然后给你钱");
        JJ.get();
    }
    public synchronized void get()
    {
        System.out.println("小强说:省下10块钱");
    }
}
public class DeadLock implements Runnable{
    private Jian JJ=new Jian();
    private XiaoQiang xq=new XiaoQiang();

    public DeadLock()
    {
        new Thread(this).start();//子线程;
        xq.say(JJ);//主线程;
    }

    @Override
    public void run() {
          JJ.say(xq);
    }

    public static void main(String[] args) {
        new DeadLock();
    }
}