多线程——同步方法解决两种线程问题

package cd_one.code12.runnablesafe;

class SaleTicket1 implements Runnable{
    int ticket = 100;
    boolean isFlag = true;
    @Override
    public  void run() {

        while(isFlag){
                show();
        }
    }
    public synchronized void show(){
        if(ticket > 0){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket);
            ticket--;
        }else{
           isFlag = false;
        }
    }
}
public class WindowTest1 {
    public static void main(String[] args) {
        SaleTicket1 s = new SaleTicket1();
        Thread t1 = new Thread(s);
        Thread t2 = new Thread(s);
        Thread t3 = new Thread(s);
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}

package cd_one.code12.exer7;

class Window1 extends Thread{
    static int ticket = 100;
    static boolean isFlag = true;
    static Object obj = new Object();
    @Override
    public void run() {
        while(true){

            show();
        }
    }
    public static synchronized void show(){
        if(ticket > 0){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace  ();
            }
            System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket);
            ticket--;
        }else{
           isFlag = false;
        }
    }
}
public class WindowTest1 {
    public static void main(String[] args) {

        Window1 w1 = new Window1();
        Window1 w2 = new Window1();
        Window1 w3 = new Window1();
        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");
        w1.start();
        w2.start();
        w3.start();

    }
}

package cd_one.code12.exer6;

class Window extends Thread{
    static int ticket = 100;
    static Object obj = new Object();
    @Override
    public void run() {
        while(true){
            synchronized (Window.class){
                if(ticket > 0){
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "售票,票号为:" + ticket);
                    ticket--;
                }else{
                    break;
                }
            }
        }
    }
}
public class windowTest {
    public static void main(String[] args) {

        Window w1 = new Window();
        Window w2 = new Window();
        Window w3 = new Window();
        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");
        w1.start();
        w2.start();
        w3.start();

    }
}

package cd_one.code12;

public class HappyNewYear {
    public static void main(String[] args) {
        for (int i = 10; i >= 0 ; i--) {
            if(i == 0){
                System.out.println("Happy New Year");
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);
        }
    }
}

package cd_one.code12.exer8;

public class AccountTest {
    public static void main(String[] args) {
        Account acct = new Account();
        Customer customer1 = new Customer(acct,"甲");
        Customer customer2 = new Customer(acct,"乙");
        customer1.start();
        customer2.start();
    }

}
class Account{
    private double balance;
    public synchronized void deposit(double amt){
        if(amt > 0){
            balance += amt;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"存钱1000块,余额为:" +balance);
    }
}
class Customer extends Thread{
    Account account;
    public Customer(Account acct){
        this.account = acct;
    }
    public Customer(Account acct,String name){
        super(name);
        this.account = acct;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            account.deposit(1000);
        }
    }
}

package cd_one.code12.exer8;

public class AccountTest {
    public static void main(String[] args) {
        Account acct = new Account();
        Customer customer1 = new Customer(acct,"甲");
        Customer customer2 = new Customer(acct,"乙");
        customer1.start();
        customer2.start();
    }

}
class Account{
    private double balance;
    public synchronized void deposit(double amt){
        if(amt > 0){
            balance += amt;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"存钱1000块,余额为:" +balance);
    }
}
class Customer extends Thread{
    Account account;
    public Customer(Account acct){
        this.account = acct;
    }
    public Customer(Account acct,String name){
        super(name);
        this.account = acct;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            account.deposit(1000);
        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃炫迈的绮绮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值