Try This 11-2 Using the Main Thread

UseMain.java

All Java programs have at least one thread of execution, called the main thread, which is given to the program automatically when it begins running. So far, we have been taking the main thread for granted. In this project, you will see that the main thread can be handled just like all other threads.

1. Create a file called UseMain.java.

2. To access the main thread, you must obtain a Thread object that refers to it. You do this by calling the currentThread( ) method, which is a static member of Thread. Its general form is shown here:

static Thread currentThread( )

This method returns a reference to the thread in which it is called. Therefore, if you call currentThread( ) while execution is inside ...

Get Java, A Beginner's Guide, 5th Edition, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.