面试题小练习

最近在网上看到一些线程成的面试题,正好之前没有看到过,就自己私下实现了下,有备无患
1、用3个线程循环控制打印ABC,连续10次

public class Test {
    public static void main(String[] args) {
        MajusculeABC maj = new MajusculeABC();
        Thread t_a = new Thread(new Thread_ABC(maj, 'A'));
        Thread t_b = new Thread(new Thread_ABC(maj, 'B'));
        Thread t_c = new Thread(new Thread_ABC(maj, 'C'));
        t_a.start();
        t_b.start();
        t_c.start();
    }
}
/**
 * 通过 MajusculeABC对象来控制循环次数,因为在线程做了条件处理,所以每个数字都只有一个线程能执行打印
 */
class MajusculeABC {
    private final static AtomicInteger sum = new AtomicInteger(30);

    public  int getSum() {
        return sum.get();
    }

    public  void deSum() {
        sum.decrementAndGet();
    }
}

class Thread_ABC implements Runnable {
    private MajusculeABC maj;
    private char name;

    Thread_ABC(MajusculeABC maj, char name) {
        this.maj = maj;
        this.name = name;
    }

    @Override
    public void run() {
        int sum = maj.getSum();
        while (sum > 0) {
            if (name == 'A' && sum % 3 == 0) {
                System.out.print(name);
                maj.deSum();
            }
            if (name == 'B' && sum % 3 == 2) {
                System.out.print(name);
                maj.deSum();
            }
            if (name == 'C' && sum % 3 == 1) {
                System.out.print(name);
                maj.deSum();
            }
            sum = maj.getSum();
        }
        System.out.println();
    }
}

2、实现死锁

public class DeadLock {
    private static final String a = "A";
    private static final String b = "B";

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

    /**
     * 实现死锁的思路,需要同时持有2把或者多把锁才能执行任务
     * 2个线程竞争,每个线程都只能拿到一把锁,都需要等待另外的线程去持有锁
     */
    public void deadLock() {
        Thread thread1 = new Thread(()->{
            synchronized (a){
                System.out.println(Thread.currentThread().getName()+" get a lock");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (b){
                    System.out.println(Thread.currentThread().getName()+" get b lock");
                    System.out.println(b);
                }
            }
        },"thread1");

        Thread thread2 = new Thread(()->{
            synchronized (b){
                System.out.println(Thread.currentThread().getName()+" get b lock");
                synchronized (a){
                    System.out.println(Thread.currentThread().getName()+" get a lock");
                    System.out.println(a);
                }
            }
        },"thread2");
        thread1.start();
        thread2.start();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值