Вы находитесь на странице: 1из 2

University of Wollongong in Dubai

Week 11

CSCI213: Java Programming and the Internet Lab 10 Topic: Threads and Timer class

Objectives: Learn the following concepts through Java Programming Understanding the purpose of thread and timer classes Implementing thread and timer in Java.

Thread A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. Each thread may or may not also be marked as a daemon. When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). There are two ways to create a new thread of execution. First Way: One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. Exercise 1 Download GreetingThread.java. Read the program carefully and answer the following questions: a. Is any exception is being caught? If yes, what type? b. Which method starts the execution of threads? c. Change the REPEATITIONS to 20 and execute the program again. Do you see any difference? d. Include a call to the sleep method for a certain DELAY for each thread. Where do you think this method should be called? e. Do you think any of these threads have a priority? If yes, what do you think is the priority? f. Modify the code to display the priority of each thread. g. Modify the code again set High priority on thread 1.

-1-

University of Wollongong in Dubai

Week 11

Second Way: The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. Exercise 2 Download GreetingThreadInterface.java from online resources. Read and compare this program with the earlier version. Write down all the differences you observe.

Processing Timer Events The Timer class in the javax.swing package generates a sequence of events, spaced apart at even time intervals. This is useful whenever you want to have an object updated in regular intervals. When a timer event occurs, the timer needs to notify some object, called the event listener. The designers of the Timer class had no idea how you would want to use the Timer, yet they had to specify a type for the listener object and call a specific method. For this purpose they chose the ActionListener interface. Exercise 3 Download TimerTest.java. Study the program carefully to answer the following questions: b. What type of constructor is being used to invoke the Timer class? c. Which two methods are being used from this class? d. Is it necessary to associate the Timer class with ActionListener class only? e. Are there any threads involved in the Timer class? Read the description from the API to answer this question. Exercise 4 Write a program to create a rectangle and change its color and size (increase) every 1 second. The color has to be determined randomly and this should be repeated atleast 10 times. The rectangle size should increase 10 times. Exercise 5 Write a program using threads such that the current date and system time can be shown. Test the program by creating 3 threads with a delay of 5 seconds between each. Every thread needs to be killed after its execution is over.

-2-

Вам также может понравиться