多线程问题, 用synchronized语句来解决

多线程问题必须满足这三个条件才会触发
1.多线程
2.共享数据
3.有多条语句操作共享数据
用同步代码块的弊端,每个线程都要判断一下是否上了锁,耗费资源
SellTicket类

点击查看代码
package it_06;

public class SellTicket implements Runnable{
    private int ticket = 100;
    Object obj =new Object();
    @Override
    public void run() {
        while(true) {
            synchronized (obj) {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + "正出售第" + ticket + "张");
                    ticket--;

                }
            }
        }
    }
}

main
点击查看代码
package it_06;

public class Demo10 {
    public static void main(String[] args) {
        SellTicket sellTicket = new SellTicket();
        Thread t1 =new Thread(sellTicket,"A窗口");
        Thread t2 =new Thread(sellTicket,"B窗口");
        Thread t3 =new Thread(sellTicket,"C窗口");

        t1.start();
        t2.start();
        t3.start();
    }
}

改成用同步方法
点击查看代码
package it_06;

public class SellTicket implements Runnable{
    private int ticket = 100;
    private int x=0;
    Object obj =new Object();
    @Override
    public void run() {
        while (true) {
            if (x % 2 == 0) {
//                synchronized (obj) {
                synchronized (this){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    if (ticket > 0) {
                        System.out.println(Thread.currentThread().getName() + "正出售第" + ticket + "张");
                        ticket--;
                    }
                }
            }else{
                sellTicket();
            }
        }
    }

    private synchronized void sellTicket() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "正出售第" + ticket + "张");
                ticket--;

            }
    }
}

如果是同步静态方法
点击查看代码
package it_06;

public class SellTicket implements Runnable{
    private static int ticket = 100;
    private int x=0;
    Object obj =new Object();
    @Override
    public void run() {
        while (true) {
            if (x % 2 == 0) {
//                synchronized (obj) {
                synchronized (SellTicket.class){
                    if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                        System.out.println(Thread.currentThread().getName() + "正出售第" + ticket + "张");
                        ticket--;
                    }
                }
            }else{
                sellTicket();
            }
            x++;
        }
    }

    private static synchronized void sellTicket() {
        if (ticket > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
                System.out.println(Thread.currentThread().getName() + "正出售第" + ticket + "张");
                ticket--;

            }
    }
}

posted @ 2025-04-25 21:08  lfqyj  阅读(17)  评论(0)    收藏  举报