多线程(巩固练习2)

目录

  1. 现将加和减的方法写出来,在定义四个线程。
  2. 利用while(true)可以一直进行抢占。
代码:
class OneThread implements Runnable {
    private Data data;

    public OneThread(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        while (true) {
            try {
                this.data.add();
                System.out.println(Thread.currentThread().getName() + "--加--:" + this.data.getTicket());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (this.data.getTicket() >= 600) {
                System.out.println("add()胜利");
                break;
            } else if (this.data.getTicket() <= 400) {
                System.out.println("add()失败");
                break;
            }

        }
    }
}

class TwoThread implements Runnable {
    private Data data;

    public TwoThread(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        while (true) {
            try {
                this.data.add();
                System.out.println(Thread.currentThread().getName() + "--加--:" + this.data.getTicket());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (this.data.getTicket() >= 600) {
                System.out.println("add()胜利");
                break;
            } else if (this.data.getTicket() <= 400) {
                System.out.println("add()失败");
                break;
            }

        }
    }
}

class ThreeThread implements Runnable {
    private Data data;

    public ThreeThread(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        while (true) {
            try {
                this.data.less();
                System.out.println(Thread.currentThread().getName() + "==减==:" + this.data.getTicket());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (this.data.getTicket() >= 600) {
                System.out.println("less()失败");
                break;
            } else if (this.data.getTicket() <= 400) {
                System.out.println("less()胜利");
                break;
            }
        }
    }
}

class FourThread implements Runnable {
    private Data data;

    public FourThread(Data data) {
        this.data = data;
    }

    @Override
    public void run() {
        while (true) {
            try {
                this.data.less();
                System.out.println(Thread.currentThread().getName() + "==减==:" + this.data.getTicket());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (this.data.getTicket() >=600) {
                System.out.println("less()失败");
                break;
            } else if (this.data.getTicket() <= 400) {
                System.out.println("less()胜利");
                break;
            }

        }
    }
}

class Data {
    private int ticket = 500;

    public void setTicket(int ticket) {
        this.ticket = ticket;
    }

    public int getTicket() {
        return this.ticket;
    }

    public synchronized void add() throws InterruptedException {
        this.setTicket(++this.ticket);
        Thread.sleep(10);
    }

    public synchronized void less() throws InterruptedException {
        this.setTicket(--this.ticket);
        Thread.sleep(10);
    }
}

public class Demo01 {
    public static void main(String[] args) {
        Data data = new Data();
        OneThread one = new OneThread(data);
        TwoThread two = new TwoThread(data);
        ThreeThread three = new ThreeThread(data);
        FourThread four = new FourThread(data);
        new Thread(one, "线程一").start();
        new Thread(two, "线程二").start();
        new Thread(three, "线程三").start();
        new Thread(four, "线程四").start();
    }
}

结果:
线程三====403
线程四====402
线程一----403
线程四====402
线程三====401
线程二----402
线程三====401
less()胜利
线程四====400
线程一----401
add()失败
线程四====400
线程二----401
add()失败
线程四====400
less()胜利

四个线程,线程加减法,循环输出加一和减一

步骤:
  1. 定义一个类,写出加法和减法的方法
  2. 在main()内些Lamdba表达式定义四个线程进行加法和减法的循环输出
代码:
class MessageA {
    private int count = 0;
    private boolean isFlag = false;

    //true表示加法
    //false表示减法
    public synchronized void sub() {
        synchronized (this) {
            if (isFlag) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            this.count--;
            System.out.println("【" + Thread.currentThread().getName() + "】减" + this.count);
            isFlag = true;
            this.notify();
        }
    }

    public synchronized void add() {
        synchronized (this) {
            if (!isFlag) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            this.count++;
            System.out.println("【" + Thread.currentThread().getName() + "】加" + this.count);
            isFlag = false;
            this.notify();
        }
    }
}

public class LamdbaA {
    public static void main(String[] args) {
        MessageA message = new MessageA();
        for (int i = 1; i <= 4; i++) {
            if (i % 2 == 0) {
                new Thread(() -> {
                    for (int j = 0; j < 50; j++) {
                        message.sub();
                    }
                }, "线程" + i).start();
            } else {
                new Thread(() -> {
                    for (int j = 0; j < 50; j++) {
                        synchronized (message) {
                            message.add();
                        }
                    }
                }, "线程" + i).start();
            }
        }
    }
}
结果:
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
【线程4】减-1
【线程3】加0
出现的问题:

不先进行加法,让线程等待,就会出现错误,输出的数据乱套,不知道是哪出现了问题。

【线程0】减-1
【线程2】减-2
【线程3】加-1
【线程1】加0
【线程0】减-1
【线程2】减-2
【线程3】加-1
【线程1】加0
【线程2】减-1
【线程1】加0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值