java多线程面试题(上机代码实现)

1、编写线程、实现子线程先循环10次、接着主线程循环20、再接着子线程循环10次、主线程循环20次、反复进行50次

package com.interview_questions.first;

public class OrderThread {

	private boolean flag=false;
	
	//子线程
	public synchronized void sub(){
		while(flag){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		for(int i=0;i<10;i++){
			System.out.println("子线程"+i);
		}
		flag = true;
		this.notify();
	}
	
	//主线程
	public synchronized void main(){
		while(!flag){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		for(int i=0;i<20;i++){
			System.out.println("主线程"+i);
		}
		flag = false;
		this.notify();
	}
}
package com.interview_questions.first;

public class CallThread implements Runnable {
	private OrderThread orderThread;
	public CallThread(OrderThread orderThread){
		this.orderThread=orderThread;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<50;i++){
			orderThread.sub();
		}
	}

}
package com.interview_questions.first;

public class CallThread2 implements Runnable {
	private OrderThread orderThread;
	public CallThread2(OrderThread orderThread){
		this.orderThread=orderThread;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<50;i++){
			orderThread.main();
		}
	}

}
package com.interview_questions.first;


public class Test {
	public static void main(String[] args) {
		OrderThread orderThread = new OrderThread();
		CallThread callThread = new CallThread(orderThread);
		CallThread2 callThread2 = new CallThread2(orderThread);
		Thread t = new Thread(callThread);
		Thread t2 = new Thread(callThread2);
		t.start();
		t2.start();
	}
}

2、使用Lock和Condition实现子线程先循环10次、接着主线程循环20、再接着子线程循环10次、主线程循环20次、反复进行50次

package com.interview_questions.second;

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

public class OrderThread {
    
	private boolean flag = false;
	Lock lock = new ReentrantLock();
	Condition con=lock.newCondition();
	
	//子线程
	public void sub(){
		lock.lock();
		try{
			while(flag){
				try {
					con.await();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			for(int i=0;i<10;i++){
				System.out.println("子线程"+i);
			}
			flag = true;
			con.signal();
		}finally{
			lock.unlock();
		}
	}
	
	//主线程
	public void main(){
		lock.lock();
		try{
			while(!flag){
				try {
					con.await();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			for(int i=0;i<20;i++){
				System.out.println("主线程"+i);
			}
			flag = false;
			con.signal();
		}finally{
			lock.unlock();
		}
	}
}
package com.interview_questions.second;

public class CallThread implements Runnable {
	private OrderThread orderThread;
	public CallThread(OrderThread orderThread){
		this.orderThread=orderThread;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<50;i++){
			orderThread.sub();
		}
	}

}
package com.interview_questions.second;

public class CallThread2 implements Runnable {
	private OrderThread orderThread;
	public CallThread2(OrderThread orderThread){
		this.orderThread=orderThread;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int i=0;i<50;i++){
			orderThread.main();
		}
	}

}
package com.interview_questions.second;


public class Test {
	public static void main(String[] args) {
		OrderThread orderThread = new OrderThread();
		CallThread callThread = new CallThread(orderThread);
		CallThread2 callThread2 = new CallThread2(orderThread);
		Thread t = new Thread(callThread);
		Thread t2 = new Thread(callThread2);
		t.start();
		t2.start();
	}
}

3、设计4个线程、实现2个线程对i加一,2个线程对i进行减一

package com.interview_questions.thrid;

public class Test {
	private int i=0;
	private synchronized void add(){
		i++;
		System.out.println(Thread.currentThread().getName()+"加"+i);
	}
	private synchronized void sub(){
		i--;
		System.out.println(Thread.currentThread().getName()+"减"+i);
	}
	
	//实现内部类
	class First implements Runnable{
		public void run() {
			add();
		}
	}
	
	//实现内部类
	class Second implements Runnable{
		public void run() {
			sub();
		}
	}
	
	public static void main(String[] args) {
		Test test = new Test();
		First f = test.new First();
		Second s = test.new Second();
		
		for(int i = 0; i < 2; i++){
			Thread t = new Thread(f);
			t.start();
			Thread t1 = new Thread(s);
			t1.start();
		}
	}
}

4、现有的程序代码模拟产生了16个日志对象,并且需要运行16秒才能打印完这些日志,请在程序中增加4个线程去调用parseLog()方法来分头打印这16个日志对象,程序只需要运行4秒即可打印完这些日志对象

package com.interview_questions.four;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockQueueTest {
    
	//parseLog方法内部的代码不能改动
    public static void parseLog(String log){
        System.out.println(log+":"+(System.currentTimeMillis()/1000));
        
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }        
    }
	
	public static void main(String[] args) {
		//定义一个线程共享的队列容器,可以使得数据由队列的一端输入,从另外一端输出
	    final BlockingQueue queue=new ArrayBlockingQueue(16);
	    //模拟生成日志
	    for(int i=0;i<16;i++){  
            final String log = ""+(i+1);
            {
                     try {
                        queue.put(log);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
        }
	    
	    for(int i=0;i<4;i++){
            new Thread(new Runnable(){

                @Override
                public void run() {
                    while(true){
                        try {
                            parseLog(queue.take());
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
                
            }).start();
        }
	}
	
    
}

你可能感兴趣的:(java)