【Java】【多线程常见面试题】

1.子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码

package Test;

public class Test {
    static class Business{
        private boolean Substop=false;

        public synchronized void sub(int i){
            while(Substop){
                try {
                    this.wait();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            for (int j = 0; j <10 ; j++) {
                System.out.println("sub run"+j+"in round"+i);
            }
            Substop=true;
            this.notifyAll();
        }
        public synchronized void main(int i){
            while (!Substop){
                try {
                    this.wait();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            for (int j = 0; j <100 ; j++) {
                System.out.println("main run"+j+"in round"+i);
            }
            Substop=false;
            this.notifyAll();
        }
    }

    public static void main(String[]  args){
        final Business business=new Business();
        for (int i = 0; i <50 ; i++) {
            final int fi=i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                   // business.sub(i);     报错  只能访问方法中定义的final类型的局部变量
                    business.sub(fi);  //当一个对象获取对象锁的时候 其另外的同步方法都会暂停执行
                }
            }).start();
            business.main(fi);
        }
    }
}


package Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
    static class Business{
        Lock lock=new ReentrantLock();
        Condition condition=lock.newCondition();
        private boolean Substop=false;
        public  void sub(int i){
            lock.lock();
            try{
                while(Substop){
                    try {
                        condition.await();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                for (int j = 0; j <10 ; j++) {
                    System.out.println("sub run"+j+"in round"+i);
                }
                Substop=true;
                condition.signalAll();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }
        public  void main(int i){
            lock.lock();
            try {
                while (!Substop){
                    try {
                       condition.await();
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
                for (int j = 0; j <100 ; j++) {
                    System.out.println("main run"+j+"in round"+i);
                }
                Substop=false;
                condition.signalAll();
            }catch (Exception e){
                    e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }
    }

    public static void main(String[]  args){
        final Business business=new Business();
        for (int i = 0; i <50 ; i++) {
            final int fi=i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                   // business.sub(i);     报错  只能访问方法中定义的final类型的局部变量
                    business.sub(fi);  //当一个对象获取对象锁的时候 其另外的同步方法都会暂停执行
                }
            }).start();
            business.main(fi);
        }
    }
}

package Test;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {
    static class Business{
      LinkedBlockingQueue<Integer> queue1=new LinkedBlockingQueue<>(1);
      LinkedBlockingQueue<Integer>  queue2=new LinkedBlockingQueue<>(1);

      {
            try{
                queue2.put(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
      }

        public  void sub(int i){
            try{
                queue1.put(1);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            for (int j = 0; j <10 ; j++) {
                    System.out.println("sub run"+j+"in round"+i);
                }
            try {
                queue2.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        public  void main(int i){
            try{
                queue2.put(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int j = 0; j <99 ; j++) {
                System.out.println("main run " + j + " in round " + i);
            }
            try{
                queue1.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[]  args){
        final Business business=new Business();
        for (int i = 0; i <50 ; i++) {
            final int fi=i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                   // business.sub(i);     报错  只能访问方法中定义的final类型的局部变量
                    business.sub(fi);  //当一个对象获取对象锁的时候 其另外的同步方法都会暂停执行
                }
            }).start();
            business.main(fi);
        }
    }
}


2.编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC

package Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test{
    static class Innerclass{
        Lock  lock=new ReentrantLock();
        Condition conditionA=lock.newCondition();
        Condition conditionB=lock.newCondition();
        Condition conditionC=lock.newCondition();
        private String name="A";

        public void printA(){
            lock.lock();
            try {
                if (!name.equals("A")){
                    conditionA.await();
                }
                System.out.print(Thread.currentThread().getName());
                Thread.sleep(1000);
                name="B";
                conditionB.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }
        public void printB(){
            lock.lock();
            try {
                if (!name.equals("B")){
                    conditionB.await();
                }
                System.out.print(Thread.currentThread().getName());
                Thread.sleep(1000);
                name="C";
                conditionC.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }
        public void printC(){
            lock.lock();
            try {
                if (!name.equals("C")){
                    conditionC.await();
                }
                System.out.print(Thread.currentThread().getName());
                Thread.sleep(1000);
                name="A";
                conditionA.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }
    }

    public static void init(){
        final Innerclass innerclass=new Innerclass();
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <10 ; i++) {
                    innerclass.printA();
                }
            }
        });
        thread.setName("A");

      Thread thread2=new Thread(new Runnable() {
          @Override
          public void run() {
              for (int i = 0; i <10 ; i++) {
                  innerclass.printB();
              }
          }
      });
      thread2.setName("B");

        Thread thread3=new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <10 ; i++) {
                    innerclass.printC();
                }
            }
        });
        thread3.setName("C");

        thread.start();
        thread2.start();
        thread3.start();
    }

    public static void main(String[] args) {
        Test test=new Test();
        test.init();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值