难度级别: 中等
程序
程序一
1) 以下程序的输出是什么?
public class Test implements Runnable { public void run() { System.out.printf("HY "); System.out.printf("haiyong "); } public static void main(String[] args) { Test obj = new Test(); Thread thread = new Thread(obj); thread.start(); System.out.printf("haiyong "); try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("for "); } }
a) HY Haiyong Haiyong for
b) Haiyong HY Haiyong for
c) a 或 b
d) a 和 b 在一起
程序二
2) 以下程序的输出是什么?
public class Test implements Runnable { public void run() { System.out.printf("HY "); } public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(new Test()); thread1.start(); thread1.start(); System.out.println(thread1.getState()); } }
a) HY HY TERMINATED
b) HY TERMINATED
c) 编译错误
d) 运行时错误
程序三
3) 以下程序的输出是什么?
public class Test extends Thread implements Runnable { public void run() { System.out.printf("HY "); } public static void main(String[] args) throws InterruptedException { Test obj = new Test(); obj.run(); obj.start(); } }
a) 运行时错误
b) 编译错误
c) HY HY
d) 以上都不是
程序四
4) 以下程序的输出是什么?
class myThread implements Runnable { public void run() { Test.obj.notify(); } } public class Test implements Runnable { public static Test obj; private int data; public Test() { data = 10; } public void run() { obj = new Test(); obj.wait(); obj.data += 20; System.out.println(obj.data); } public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(new Test()); Thread thread2 = new Thread(new myThread()); thread1.start(); thread2.start(); System.out.printf(" HY - "); } }
a) 30 HY –
b) HY – 30
c) HY –
d) 编译错误
程序五
5) 以下程序的输出是什么?
import java.util.concurrent.*; public class Test implements Runnable { public static CyclicBarrier barrier = new CyclicBarrier(3); public void run() { System.out.printf(" HY "); try { barrier.await(); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } } public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(new Test()); Thread thread2 = new Thread(new Test()); thread1.start(); thread2.start(); System.out.printf(" haiyong "); try { barrier.await(); } catch (InterruptedException | BrokenBarrierException e) { e.printStackTrace(); } System.out.printf(" End "); } }
a) haiyong HY HY End
b) HY haiyong HY End
c) HY HY haiyong End
d) 以上所有
文章后半部分是程序的输出及解析
输出及解析
程序一输出
答案:
(c)
说明:
从“thread.start()”语句中,我们有两个线程主线程和“thread”线程。所以要么输出“HY”,要么打印“haiyong”,这取决于哪个线程,线程调度程序调度。对于(a),调用 start() 方法后的父线程被暂停,线程调度器调度子线程,然后子线程完成它的执行。在此之后,父线程被调度。对于 (b),父线程调用 start() 方法但继续执行并在控制台上打印。当调用 join() 方法时,父线程必须等待其子线程完成执行。线程调度器在父线程等待子线程完成时调度子线程。
程序二输出
答案:
(d)
说明:
在线程上调用 start() 方法会将线程移动到 RUNNABLE 状态。但是在已经启动的线程上调用 start() 方法会引发 IllegalThreadStateException,因为该线程已经处于 RUNNABLE 状态。
程序三输出
答案:
(c)
说明:
Test 类扩展了已实现 start() 方法的 Thread 类。因此,在扩展 Thread 类的对象上调用 start() 会调用程序中定义的 run() 方法。
程序四答案
回答 :
(d)
说明:
在调用wait() 方法之前,对象必须先获取锁。同样,wait() 方法会抛出Checked异常(InterruptedException),我们必须将其包含在 try-catch 块中或抛出它。
程序五答案
回答 :
(d)
说明:
对于(a),父线程一直执行直到到达屏障。然后调度子线程。对于(b),线程调度器调度器thread1。一旦到达屏障,父线程就会被调度。一旦父线程到达屏障,线程 2 就会被调度。对于(c),两个子线程都被调度。最后,当每个子线程到达屏障时,父线程就会被调度。
以上就是本篇文章的所有内容了