导航菜单
首页 >  » 正文

Java线程面试题,要求写出输出结果吗

Java线程在各大IT企业的面试中常常成为一个重要的考察点。在线程面试中,往往会出现一些需要写出输出结果的考题,那么这些题目背后的考点是什么?本文将结合具体的题目分析一下。

题目一

在下面的代码中,当程序运行时会输出什么结果?

class MyThread extends Thread { 
    public static synchronized void foo() { 
        System.out.println("foo"); 
    } 
    public static synchronized void bar() { 
        System.out.println("bar"); 
        foo(); 
    } 
    public void run() { 
        bar(); 
    } 
} 
public class TestThread { 
    public static void main(String args[]) { 
        Thread mt = new MyThread(); 
        mt.start(); 
    } 
} 

输出结果:bar foo

解析:MyThread的run()方法中调用了bar()方法,在bar()方法中又调用了foo()方法。由于bar()方法和foo()方法都是static synchronized方法,因此获取线程锁的顺序是不同的,即先获取bar()方法的线程锁,后获取foo()方法的线程锁,所以输出结果为bar foo。

题目二

下面这段代码的输出结果是什么?

class MyThread extends Thread { 
    public MyThread(String name) { 
        super(name); 
    } 
    public void run() { 
        System.out.println(Thread.currentThread().getName()); 
    } 
} 
public class TestThread { 
    public static void main(String args[]) { 
        Thread t1 = new MyThread("t1"); 
        ThreadGroup tg = new ThreadGroup("group"); 
        Thread t2 = new Thread(tg, new MyThread("t2"), "t2"); 
        t1.start(); 
        t2.start(); 
    } 
} 

输出结果:t1 t2

解析:在t1中调用getName()方法会返回线程名t1,在t2中调用getName()方法会返回线程名t2。

题目三

以下代码会输出什么?

public class TestThread { 
    public static void main(String args[]) { 
        Thread th = new Thread(new Runnable() { 
            public void run() { 
                try { 
                    System.out.print("foo"); 
                    Thread.sleep(1000); 
                    System.out.print("bar"); 
                } catch (InterruptedException e) { 
                    e.printStackTrace(); 
                } 
            } 
        }); 
        th.start(); 
    } 
} 

输出结果:foo(等待1秒)bar

解析:在run()方法中,首先输出"foo",然后调用Thread.sleep(1000)等待1秒,最后输出"bar"。

题目四

下面这段代码的输出结果是什么?

class MyThread extends Thread { 
    public void run() { 
        for(int i = 0; i < 20000; i  ) { 
            System.out.print("a"); 
        } 
    } 
} 
public class TestThread { 
    public static void main(String args[]) { 
        MyThread mt = new MyThread(); 
        mt.start(); 
        for(int i = 0; i < 20000; i  ) { 
            System.out.print("b"); 
        } 
    } 
} 

输出结果:一串随机排列的a和b

解析:由于MyThread与主线程是并行执行的,因此在输出字符串时两个线程的输出顺序是不确定的。

题目五

以下代码输出结果是什么?

public class TestThread { 
    public static void main(String args[]) { 
        Thread th1 = new Thread(new Runnable() { 
            public void run() { 
                System.out.println(1); 
            } 
        }); 
        Thread th2 = new Thread(new Runnable() { 
            public void run() { 
                System.out.println(2); 
            } 
        }); 
        th1.start(); 
        th2.start(); 
        System.out.println(3); 
    } 
} 

输出结果:3 1 2

解析:由于th1和th2的输出是异步的,因此程序的执行顺序与线程启动的顺序无关。

通过以上这些面试题,我们可以了解到一些Java多线程的基本知识点,包括线程锁、线程组、线程等待、线程启动的顺序等。在日常工作中,多线程的应用也是非常重要的,需要我们不断学习积累经验,才能够更好的应对各种开发需求。