This is the 20th day of my participation in the August More Text Challenge

Difficulty Level: Medium

The program

Program a

1) What is the output of the following program?

public class Test implements Runnable
{
	public void run(a)
	{
		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 "); }}Copy the code

A) HY Haiyong b) HY Haiyong C) a or B D) a and B

Click here to jump to the answer


Program 2

2) What is the output of the following program?

public class Test implements Runnable
{
	public void run(a)
	{
		System.out.printf("HY ");
	}
	public static void main(String[] args) throws InterruptedException
	{
		Thread thread1 = new Thread(newTest()); thread1.start(); thread1.start(); System.out.println(thread1.getState()); }}Copy the code

A) HY HY TERMINATED B) HY TERMINATED C) compiler error D) runtime error

Click here to jump to the answer


Application of three

3) What is the output of the following program?

public class Test extends Thread implements Runnable
{
	public void run(a)
	{
		System.out.printf("HY ");
	}
	public static void main(String[] args) throws InterruptedException
	{
		Test obj = newTest(); obj.run(); obj.start(); }}Copy the code

A) runtime error B) compile error C) HY HY D) None of the above

Click here to jump to the answer


Application of four

4) What is the output of the following program?

class myThread implements Runnable
{
	public void run(a)
	{ Test.obj.notify(); }}public class Test implements Runnable
{
	public static Test obj;
	private int data;
	public Test(a)
	{
		data = 10;
	}
	public void run(a)
	{
		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 - "); }}Copy the code

A) 30 HY — B) HY — 30 C) HY — d) Error

Click here to jump to the answer


Application of five

5) What is the output of the following program?

import java.util.concurrent.*;

public class Test implements Runnable
{
	public static CyclicBarrier barrier = new CyclicBarrier(3);
	public void run(a)
	{
		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 "); }}Copy the code

A) HY HY End B) HY HY End C) HY HY End D) All of the above

Click here to jump to the answer


The second part is the output and analysis of the program


Output and parsing

Program one output

The answer:

(c)Copy the code

Description:

From the “thread.start()” statement, we have two threads: the main thread and the “thread” thread. So either print “HY” or print “haiyong”, depending on which thread the thread scheduler dispatches. For (a), the parent thread is suspended after the start() method is called, the thread scheduler schedules the child thread, and the child thread completes its execution. After that, the parent thread is scheduled. For (b), the parent thread calls the start() method but continues to execute and print on the console. When the join() method is called, the parent thread must wait for its child thread to complete execution. The thread scheduler schedules child threads while the parent thread waits for the child threads to complete.


Program two output

The answer:

(d)Copy the code

Description:

Calling the start() method on the thread moves it to the RUNNABLE state. But on the threads are started calling start () method will cause IllegalThreadStateException, because the thread is already in the RUNNABLE state.


Program three output

The answer:

(c)
Copy the code

Description:

The Test class extends the Thread class that implements the start() method. Therefore, calling start() on an object extending Thread calls the run() method defined in the program.


Procedure 4 Answer

Answer:

(d)
Copy the code

Description:

An object must acquire the lock before calling wait(). Similarly, the wait() method throws a Checked exception (InterruptedException), which we must either include in a try-catch block or throw.


Procedure 5 Answer

Answer:

(d)
Copy the code

Description:

For (a), the parent thread executes until the barrier is reached. The child threads are then scheduled. For (b), the thread scheduler dispatches thread1. Once the barrier is reached, the parent thread is scheduled. Once the parent thread reaches the barrier, thread 2 is scheduled. For (c), both child threads are scheduled. Finally, when each child thread reaches the barrier, the parent thread is scheduled.


That’s all for this article

Related articles:

Java program Java exercises 】 【 | at the output of the first set of (analysis) the output of the Java program Java exercises 】 【 | second (analysis) the output of a Java program | Java exercises 】 【 a third (including parsing) [Java exercises] | at the output of the Java program Fourth set (including the parse) the output of a Java program | Java exercises 】 【 5 sets (including parsing) the output of the Java program Java exercises 】 【 | 6 sets (including parsing) the output of a Java program | Java exercises 】 【 7 sets (including parsing) Java Java exercises 】 【 Program’s output | 8 sets (including parsing) the output of a Java program | Java exercises 】 【 9 sets of (including the parse) the output of a Java program | Java exercises 】 【 10 sets (recycling) the output of a Java program | Java exercises 】 【 11 sets of (including parsing) The output of a Java program | Java exercises 】 【 12 sets (including parsing) the output of a Java program | Java exercises 】 【 13 sets (set) the output of a Java program | Java exercises 】 【 14 sets (constructor) [Java exercises] | at the output of the Java program Set 15 (Internal)

I’ve been writing tech blogs for a long time and this is one of my tech posts/tutorials. Hope you like it! Here is a summary of all my original and works of source: GitHub, and this is my recently just built blog: Haiyong. Site, there is no content, put some HTML games, interested can try, source code can be their own F12 copy, or directly find me to.

If you really learn something new from this article, like it, bookmark it and share it with your friends. 🤗 and finally, don’t forget ❤ or 📑.