Java多线程的一系列问题

a)Add code to the following code fragment to: (1) create a thread instance and let this instance conduct the task specified in TaskClass; (2) execute the run()method in TaskClass by starting the thread.

public class ThreadTest1{
    public static void main(String[] args) {
TaskClass task = new TaskClass ();
Thread a = new Thread(task);
a.start();
}

}

class TaskClass implements Runnable {
    public void run() {
System.out.println("This is a thread test");

} 

}

b) Add code to the following code fragment to create and start 2 thread instances which can execute the run method.

public class ThreadTest2 extends Thread { int id = 0;
public static void main(String[] args) {
      Thread a = new ThreadTest2();
      Thread b = new ThreadTest2();
      a.start();
      b.start();
}
public void run() {
      press(id);
}
public void press(int id) {
      System.out.print(id);
++id;
System.out.print(id);
}
}

c) Please explain how synchronized methods can help you in controlling threads, and how the join method can help you in controlling threads. When will these 2 methods give you different results?

1、To make a method synchronized: add the synchronized keyword to its declaration
When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

d)Which output(s) from A to F is/are possibly printed out from the following code fragment? (There can be more than 1 correct choice.)

public class PingPongInterrup extends Thread{
    private String word;
    private int delay;
    public PingPongInterrup(String whatToSay, int delayTime){
        word = whatToSay;
        delay = delayTime;
    }    public static void main(String[] args){
    	Thread ping = new PingPongInterrup("ping", 500); 
    	Thread pong = new PingPongInterrup("PONG", 1000); 
    	ping.start();
    	pong.start();
    	pong.interrupt();
    	} 
    public void run(){
        for(int i=1;i<=2;i++){
        	System.out.println( i+": "+word+" interrupted? "+interrupted());
        	try{
        	     Thread.sleep(delay);
        	} catch 	(InterruptedException e){ }
          }    
    }
    
}

这里要提到一点,sleep和interrupt之间的关系

sleep是阻塞,interrupt是打断

只有阻塞的线程被打断了,才会报异常,换言之,只有这样,catch之后的代码才会起作用

因此我们可以来分析一下这道题

因为ping.start()

pong.start()

pong.interrupt()

这里任意一种情况都可以先放在之前,因为是同时开始的,假设ping.start()放在前,那就会产生以下情况

1:ping interrupted? false     显然ping并没有被打断,在这之后ping会发生阻塞,阻塞时常假设500(delay)

那么显然要pong.start()或者pong.interrupt()开始了

之后就会有两种情况

1:pong interrupted? true  (这里是pong.interrupt()执行)    或者    1:pong interrupted? false    (这里是pong.start()执行)

如果是第一种情况,那么之后发生阻塞,这里阻塞是1000(delay),大于ping的500,因此ping就可以执行第二次循环,那之后仅有一种情况,2:ping interrupted? false,再之后即是正常执行的pong , 2:pong interrupted? false

如果是第二种情况,那么之后发生阻塞,与上面情况类似,ping执行第二次循环,2:ping interrupted? false ,在这之后即是pong.interrupt()执行,这里就要用到“只有阻塞的线程被打断了,才会报异常”这个概念,所以这里catch之后的代码会起作用,正因为其起到作用,而且我又没有去处理该异常,就会继续执行循环,线程会重新跑,所以这里interrupt就变成false了

所以以上就会产生这两种情况

first:1:ping interrupted? false

1:pong interrupted? true

2:ping interrupted? false

2:pong interrupted? false

second:

1: ping interrupted? false

1: PONG interrupted? false

2: PONG interrupted? false

2: ping interrupted? false

那么后面的情况就是pong.start()先,或者pong.interrupt()先

pong.start()先的话

即为

1: PONG interrupted? false

2: PONG interrupted? false

1: ping interrupted? false

2: ping interrupted? false

1: PONG interrupted? false

1: ping interrupted? false

2: PONG interrupted? false

2: ping interrupted? false

pong.interrupt()先的话

即为

1: PONG interrupted? true

1: ping interrupted? false

2: ping interrupted? false

2: PONG interrupted? false

猜你喜欢

转载自blog.csdn.net/qq_42615643/article/details/84941710