首页 > 代码库 > 【java】:多线程面试题

【java】:多线程面试题

经常面试的时候,让写各种乱七八糟的多线程面试题,收集了很多,有些还是挺好玩的。


 

 

1、编写程序实现,子线程循环10次,接着主线程循环20次,接着再子线程循环10次,主线程循环20次,如此反复,循环50次.

技术分享
package com.zhikui.interview;

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


/**@autor http://www.cnblogs.com/fingerboy/p/5352880.html
 * @method 编写程序实现,子线程循环10次,接着主线程循环20次,接着再子线程循环10次,主线程循环20次,如此反复,循环50次.
 */
public  class interviewTest1{
    
    public static void main(String[] args) {
         final Function fc= new Function();
        //子线程
        new Thread(new Runnable() {
            
            @Override
            public void run() {
                for(int i =0;i<50;i++){
                    fc.sub();
                }
                
            }
        }).start();
        //主线程
        for(int i =0;i<50;i++){
            fc.main();
        }
    }
}


class Function {
    private boolean flag = false;
    //Lock lock=new ReentrantLock();
    // Condition con=lock.newCondition();
    //子线程实现
    public synchronized void sub(){
        while(flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                 e.printStackTrace();
            }
        }
        for(int i =0;i<10;i++){
            System.out.println("[sub]"+i);
        }
        
        flag = true;
        this.notify();
    }
    //主线程实现
    public synchronized void main(){
        while(!flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                 e.printStackTrace();
            }
        }
        for(int i =0;i<20;i++){
            System.out.println("[main]"+i);
        }
        flag = false;
        this.notify();
    }
}
View Code

2、设计四个线程,其中两个线程每次对变量i加1,另外两个线程每次对i减1.

技术分享
package com.zhikui.interview;
/**
 * @methord设计四个线程,其中两个线程每次对变量i加1,另外两个线程每次对i减1.
 * @author http://www.cnblogs.com/fingerboy/p/5352880.html
 *
 */

public class interviewTest2 {
    
    private  int i = 0;
    
    public static void main(String[] args) {
        //执行线程
        interviewTest2 it = new interviewTest2();
        Add add = it.new Add();
        Sub sub = it.new Sub();
        for(int i=1;i<=2;i++){
             new Thread(add,"线程"+i).start();
             new Thread(sub,"线程"+i).start();
       }
    }
    
    class Add implements Runnable {
        @Override
        public void run() {
            for(int j=0;j<10;j++){
                addOne();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    class Sub implements Runnable{
        @Override
        public void run() {
            for(int j=0;j<10;j++){
                subOne();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }    
    }
    
    public synchronized void addOne(){
        i++;
        System.out.println(Thread.currentThread().getName()+"[加一的值为]"+i);
    }
    
    public synchronized void subOne(){
        i--;
        System.out.println(Thread.currentThread().getName()+"[减一的值为]"+i);
    }
}
View Code

3、T2 T3三个线程,怎样保证T2在T1执行完之后执行 T3在T2执行完之后执行

技术分享
package com.zhikui.interview;
/**
 * @methor现在有T1 T2 T3三个线程,怎样保证T2在T1执行完之后执行 T3在T2执行完之后执行
 * @author http://blog.csdn.net/caohaicheng/article/details/38071097
 *
 */
public class interviewTest3 {
    
    public static void main(String[] args) {
        interviewTest3 it = new interviewTest3();
        T1 t1 = it.new T1("t1"); 
        T1 t2 = it.new T1("t2"); 
        T1 t3 = it.new T1("t3"); 
        t1.start();
        try {
            t1.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        t2.start();
        try {
            t2.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        t3.start();
        try {
            t3.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
         
    }

    class T1 extends Thread{
        private String name;
        public T1(String name){
            this.name = name;
        }
        @Override
        public void run(){
             for(int i=0;i<5;i++){  
                    try {  
                        sleep(5);  
                    } catch (InterruptedException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                    System.out.println(this.name+"循环"+i);  
               }  
        }
    }
    
    class T2 extends Thread{
        private String name;
        public T2(String name){
            this.name = name;
        }
        @Override
        public void run(){
             for(int i=0;i<5;i++){  
                    try {  
                        sleep(5);  
                    } catch (InterruptedException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                    System.out.println(this.name+"循环"+i);  
               }  
        }
    }
    
    class T3 extends Thread{
        private String name;
        public T3(String name){
            this.name = name;
        }
        @Override
        public void run(){
             for(int i=0;i<5;i++){  
                    try {  
                        sleep(5);  
                    } catch (InterruptedException e) {  
                        // TODO Auto-generated catch block  
                        e.printStackTrace();  
                    }  
                    System.out.println(this.name+"循环"+i);  
               }  
        }
    }
    
}
View Code

4、写一个死锁的例子

技术分享
package com.zhikui.interview;
/**
 * 写一个死锁的例子
 * @author author
 */
public class interviewTest4 {
    private static Object A = new Object();
    private static Object B = new Object();
    public static void main(String[] args) {
        //第一个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    synchronized (A) {
                        synchronized (B) {
                            System.out.println("死锁A");
                        }
                    }
                }
            }
        },"T1").start();    
        
        //第二个线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    synchronized (B) {
                        synchronized (A) {
                            System.out.println("死锁B");
                        }
                    }
                }
            }
        },"T1").start();    
    }

}
View Code

5、两个线程,一个线程输出1,一个线程输出2,循环输出

技术分享
package com.zhikui.interview;
/**
 * @methor两个线程,一个线程输出1,一个线程输出2,循环输出 
 * @author http://blog.csdn.net/fufengrui/article/details/30232603
 *
 */
public class interviewTest5 {  
    public static void main(String[] args) {  
        OneThread one = new OneThread();  
        TwoThread two = new TwoThread();  
        one.start();  
        two.start();  
    }  
}  
  
class OneThread extends Thread {  
  
    @Override  
    public void run() {  
        synchronized (interviewTest5.class) {  
            while (true) {  
                System.out.println("1");  
                try {  
                    interviewTest5.class.wait();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                interviewTest5.class.notify();  
            }  
        }  
    }  
}  
  
class TwoThread extends Thread {  
  
    @Override  
    public void run() {  
        synchronized (interviewTest5.class) {  
            while (true) {  
                System.out.println("2");  
                interviewTest5.class.notify();  
                try {  
                    interviewTest5.class.wait();  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  
}  
View Code

6、有1-26个数字和a-z字母,用Java多线程实现先输出2和数字再输出2个字母

技术分享
package com.zhikui.interview;

/**
 * 有1-26个数字和a-z字母,用Java多线程实现先输出2和数字再输出2个字母
 * 
 * @author https://zhidao.baidu.com/question/201633880.html
 * 
 */
public class interviewTest6 {
    public static void main(String[] args) {
        Print p = new Print();
        new numThread(p).start();
        new charThread(p).start();
    }
}

class Print {
    boolean boo = true;
    char ch = ‘A‘;
    int num = 1;

    public synchronized void printNum() {
        if (boo) {
            try {
                wait();
            } catch (Exception e) {
            }
            System.out.print(num++);
            System.out.print(num++);
        }
        boo = false;
        notify();
        if (num == 52)
            num++;
    }

    public synchronized void printChar() {
        if (!boo) {
            try {
                wait();
            } catch (Exception e) {
            }
            System.out.print(ch++);
            System.out.print(ch++);
        }
        boo = true;
        notify();
    }
}

class numThread extends Thread {
    Print p = null;

    public numThread(Print p) {
        this.p = p;
    }

    public void run() {
        while (p.num <= 53)
            p.printNum();

    }
}

class charThread extends Thread {
    Print p = null;

    public charThread(Print p) {
        this.p = p;
    }

    public void run() {
        while (p.ch <= ‘Z‘)
            p.printChar();
    }
}
View Code

 

【java】:多线程面试题