java面试手写线程相关

1.两个线程轮番打印奇数和偶数1-100

方法一:使用单线程池来实现

public class 两个线程打印奇数偶数 {
	
	
	public static void main(String[] args) {
		
		ExecutorService pool=Executors.newSingleThreadExecutor();
		
		AtomicInteger a=new AtomicInteger(0);
		AtomicInteger b=new AtomicInteger(1);
		for(int i=0;i<50;i++) {
			pool.execute(()->System.out.println(a.getAndAdd(2)+1));
			pool.execute(()->System.out.println(b.getAndAdd(2)+1));
	
		}

		pool.shutdown();
	
	}
	

}

方法二:使用ReentrantLock()来控制

public class 线程创建 {
    private static ReentrantLock lock = new ReentrantLock();
    private static int state= 0;
    public static void main(String[] args)  {

      new Jithread().start();
      new Outhread().start();
    }



    static class  Jithread extends Thread{
        @Override
        public void run() {
            for (int i = 0; i<50;) {
                try {
                    lock.lock();
                    while (state%2==0){
                        System.out.print(state+" ");
                        state++;
                        i++;
                    }
                }finally {
                    lock.unlock();
                }
            }
        }
    }


    static class  Outhread extends Thread{
        @Override
        public void run() {
            for (int i = 0; i<50;) {
                try {
                    lock.lock();
                    while (state%2==1){
                        System.out.print(state+" ");
                        state++;
                        i++;
                    }
                }finally {
                    lock.unlock();
                }
            }
        }
    }

}

2.三个线程轮番输出10次ABC

线程池方法

public class 三个线程打印ABC{
	
	public static void main(String[] args) {
		ExecutorService pool=Executors.newSingleThreadExecutor();
		
		for(int i=0;i<10;i++) {
			pool.execute(()->System.out.print("A"));
			pool.execute(()->System.out.print("B"));
			pool.execute(()->System.out.print("C"));
			pool.execute(()->System.out.println());
		}
		pool.shutdown();
	}

}

锁方法

public class 三个线程打印ABC {
	
	static ReentrantLock lock=new ReentrantLock();
	static int state=0;
	
	public static void main(String[] args) {
		
		new ThreadA().start();
		new ThreadB().start();
		new ThreadC().start();
	}

	static  class ThreadA extends Thread{
		@Override
		public void run() {
			int sum=0;
			for(int i=0;i<10;) {
				try {
					lock.lock();
					
					while(state%3==0) {
						System.out.print("A");
						i++;
						state++;
					}	
				}finally {
					lock.unlock();
					System.out.println(sum++);
				}
			}
		}
	}
	
	static  class ThreadB extends Thread{
		public void run() {
			for(int i=0;i<10;) {
				try {
					lock.lock();
					while(state%3==1) {
						System.out.print("B");
						i++;
						state++;
					}	
				}finally {
					lock.unlock();
				}
			}
		}
	}
	
	static  class ThreadC extends Thread{
		public void run() {
			for(int i=0;i<10;) {
				try {
					lock.lock();
					while(state%3==2) {
						System.out.print("C");
						i++;
						state++;
					}	
				}finally {
					lock.unlock();
				}
			}
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值