1. 程式人生 > >【java】:多線程面試題

【java】:多線程面試題

編寫 cond func condition pri ide syn ack rri

經常面試的時候,讓寫各種亂七八糟的多線程面試題,收集了很多,有些還是挺好玩的。


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】:多線程面試題