java,线程同步,队列和锁,线程通信,线程池

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


提示:以下是本篇文章正文内容,下面案例可供参考

线程同步


​ 处理多线程问题时,多个线程访问同一对象,并且某写线程还想修改这个对象(并发问题)。 这时后我们就需要线程同步,就是一种等待机制;多个同时访问此对象时的线程进入这个 对象的等待池 形成队列;等待前面的线程使用完毕,下个再使用。

队列 和 锁(synchronized)


锁:synchronized

线程同步形成条件:队列 和 锁 (安全性)

方法里面需要修改的内容才需要锁,锁的太多,浪费资源!

代码示例一:同步方法

// 买票
public class syn {

    public static void main(String[] args) {

        BuyTivkes tivkes = new BuyTivkes();
        new Thread(tivkes,"a").start();
        new Thread(tivkes,"b").start();
        new Thread(tivkes,"c").start();
    }
}

class BuyTivkes implements Runnable {

    private int tickes = 10; // 票数
    boolean flage = true; // 标志位

    @Override
    public void run() {
        while (flage) {
            try {
                buy();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    // synchronized:同步方法,锁的是this
    private synchronized void buy() throws InterruptedException {
        // 判断是否还有票
        if (tickes <= 0) {
            flage = false;
            System.out.println("票卖完了");
            return;
        }
        Thread.sleep(100);
        System.out.println(Thread.currentThread().getName()+"--->买到了第"+tickes--+"票");
    }
}
代码示例二:同步代码块

// 模拟取钱
public class synMoney {
    public static void main(String[] args) {
        Account account = new Account(100,"建设银行卡");

        new Thread(new Drawing(account,50,"男")).start();
        new Thread(new Drawing(account,100,"女")).start();
    }
}

// 账户
class Account {
    int money; // 余额
    String name; // 卡名

    public Account(int money, String name) {
        this.money = money;
        this.name = name;
    }
}


class Drawing implements Runnable {

    Account account; // 账户

    int drawingMoney; // 取多少钱

    String name; // 取钱人名

    public Drawing(Account account, int drawingMoney, String name) {
        this.account = account;
        this.drawingMoney = drawingMoney;
        this.name = name;
    }

    /**
     * synchronized 块
     */
    @Override
    public void run() {
        // 锁同步块 锁的对象 锁要变化的量,需要增删改的对象
        synchronized (account) {
            if (account.money-drawingMoney < 0) {
                System.out.println(account.name+":"+"钱不够了================");
                return;
            }

            try {
                Thread.sleep(3000);  // 添加延时,放大问题的发生性
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            account.money = account.money - drawingMoney; // 余额
            System.out.println(account.name+":"+name+"--->取了"+drawingMoney+",余额为:"+account.money);
        }
    }
}

Lock锁


  • 可重入锁

代码示例

public class TEstLock {
    public static void main(String[] args) {
        Tickes tickes = new Tickes();
        new Thread(tickes).start();
        new Thread(tickes).start();
        new Thread(tickes).start();
    }
}

class Tickes implements Runnable {
    private int tickes = 10;
    // 定义Lock锁
    private final ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {

            try {
                lock.lock(); // 加锁
                if (tickes <= 0) {
                    return;
                }
                    Thread.sleep(1000);
                System.out.println(tickes--);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock(); // 解锁
            }

        }
    }
}

线程通信


方式一:管程法


/**
 * 线程之间的通信
 *      利用缓冲区解决:管程法
 */
public class TestPC {
    public static void main(String[] args) {
        SynContainer synContainer= new SynContainer();

        new Productor(synContainer).start();
        new Consumer(synContainer).start();
    }
}

// 生产者
class Productor extends Thread {

    SynContainer synContainer;
    public Productor(SynContainer synContainer) {
        this.synContainer = synContainer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            synContainer.push(new Chicken(i));
            System.out.println("生产了"+i+"只鸡");
        }
    }
}


// 消费者
class Consumer extends Thread {

    SynContainer synContainer;
    public Consumer(SynContainer synContainer) {
        this.synContainer = synContainer;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("消费了--->"+synContainer.pop().id+"只鸡");
        }

    }
}

// 产品
class Chicken {
    int id; // 产品编号

    public Chicken(int id) {
        this.id = id;
    }
}

// 缓冲区
class SynContainer {

    // 需要一个容器大小
    Chicken[] chickens = new Chicken[10];
    // 容器计数器
    int count = 0;

    // 生产者放入产品
    public synchronized void push(Chicken chicken) {
        // 如果容器满了,就需要等待消费者消费
        if (count == chickens.length) {
            // 消费者,消费; 生产者等待
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        // 如果没有满,就需要丢入产品
        chickens[count] = chicken;
        count++;

        // 通知消费者消费
        this.notify();
    }

    // 消费者消费产品
    public synchronized  Chicken pop() {
        // 判断能否消费
        if (count == 0) {
            // 消费者等待,生产者生产
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        // 如果可以消费
        count--;
        Chicken chicken = chickens[count];

        // 吃完了,通知生产者生产
        this.notify();
        return chicken;

    }

}

方式二:标志位解决法


/**
 * 线程之间的通信
 *      方式二:标志位解决法
 */
public class TestPC2 {
    public static void main(String[] args) {
        TV tv = new TV();

        new Player(tv).start();
        new Watcher(tv).start();
    }
}


// 生产者--->演员
class Player extends Thread {
    TV tv;
    public Player(TV tv) {
        this.tv = tv;
    }

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if (i%2==0) {
                this.tv.play("播放快乐大本营");
            } else {
                this.tv.play("播放广告:抖音纪律美好生活");
            }
        }

    }
}

// 消费者---->观众
class Watcher extends Thread {
    TV tv;
    public Watcher(TV tv) {
        this.tv = tv;
    }

    @Override
    public void run() {

        for (int i = 0; i < 20; i++) {
            this.tv.wantch();
        }
    }
}


// 产品--->节目
class TV {

    String voice; // 表演的节目
    boolean flag = true; // 标志位  T:演  F:看

    // 表演
    public synchronized void play(String voice) {

        if (!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("演员表演了:"+voice);
        // 通知 观看
        this.notify(); // 通知 唤醒
        this.voice = voice;
        this.flag = !flag;
    }

    // 观看
    public synchronized void wantch() {
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        System.out.println("观众观看了:"+voice);
        // 通知演员表演
        this.notify();
        this.flag = !flag;

    }
}

线程池


代码示例

public class TestPool {
    public static void main(String[] args) {
        // 创建线程池 newFixedThreadPool:线程池大小
        ExecutorService service = Executors.newFixedThreadPool(10);

        service.execute(new MyPool());
        service.execute(new MyPool());
        service.execute(new MyPool());
        service.execute(new MyPool());

        // 关闭连接
        service.shutdown();
    }
}

class MyPool implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值