The first approach for creating a thread by extending the Thread class b.. Exercise 2: Thread state.. Exersice 3: Method of the Thread class 4.. Exercise 4: Managing Thread: Priority...
Trang 1Bai Lab Thread 1
I Part 1: 1.5 hour
1 Exericise 1: Creating a thread
a The first approach for creating a thread by extending the Thread class
b The second approach for creating threads, where the Runnable interface is implemented
2 Exercise 2: Thread state This program show some of the states that a thread can have
Trang 23 Exersice 3: Method of the Thread class
4 Exercise 4: Managing Thread: Priority
Trang 35 Exercise 5:
a Daemon thread
b
c
d To illustrate how JVM can exit when deamon thread are alive, a single thead is created and its Daemon property
is set to true by invoking the setDaemon() method The user thread is converted to a daemon thead Now when this thread starts executing, the main method finishes its execution Thus the only thread running will be a daemon thread At this instant, the JVM exists
The same example could be tested by omitting the setDaemon() method In that case, the thread would be a normal user thread and as long as it is executing, the JVM cannot exit Hence the program will continue
indefinitely
II Part 2: 0.5 hour
Trang 4Write a program MulThread that create 2 threads, one thread with the name Marimba and the other thread named
as Jini Each thread should display its respectivename and execute after a gap of 1000 miliseconds Also the threads should display a number indicating the number of time it got a chance to execute
Hint: Use setName() method to set the name of the thread
Note: Must be used two approach to create a thread