Java Threading Assignment
Java Threading Assignment
Java Threading Assignment
This hands-on lab takes you through the basics of using Java threading.
Lab Exercises
• Exercise 1: Extending Thread class (30 minutes)
• Exercise 2: Implementing Runnable interface (30 minutes)
• Exercise 3: ThreadGroup, ThreadPriority, View all threads (30 minutes)
• Exercise 4: Synchronization (30 minutes)
• Exercise 5: Inter-thread communication (30 minutes)
• Exercise 6: Timer and TimerTask (15 minutes)
• Homework Exercise (for people who are taking Sang Shin's "Java Programming online
course")
}
}
Code-1.11: ExtendThreadClassTest0.java
PrintNameThread(String name) {
super(name);
}
7. For your own exercise, modify ExtendThreadClassTest0.java as following. Build and run
the application.
• Create and start another thread.
• Set the name of the thread as "MyOwn"
PrintNameThread pnt1 =
new PrintNameThread("A");
PrintNameThread pnt2 =
new PrintNameThread("B");
PrintNameThread pnt3 =
new PrintNameThread("C");
}
}
Code-1.21: ExtendThreadClassTest2.java
3. Write PrintNameThread.java as shown in Code-1.22 below. Note that the start() method is
invoked as part of the constructor method of the PrintNameThread class.
5. For your own exercise, modify ExtendThreadClassTest2.java as following. Build and run
the application.
• Create and start another thread.
• Set the name of the thread as "MyOwn"
return to top of the exercise
Summary
In this exercise, you have learned how to create and start a thread by extending Thread class.
2. Create and start a thread by implementing Runnable interface - start() method is in the
constructor
}
}
Code-2.11: RunnableThreadTest1.java
String name;
PrintNameRunnable(String name) {
this.name = name;
}
5. For your own exercise, do the following. Build and run the application.
• Create another class called MyOwnRunnableClass that implements Runnable
interface
• MyOwnRunnableClass displays values 1 to 10 inside its run() method
• Modify RunnableThreadTest1.java to start 2 thread instances of
MyOwnRunnableClass.
new PrintNameRunnable("B");
new PrintNameRunnable("C");
}
}
Code-2.21: RunnableThreadTest2.java
Thread thread;
PrintNameRunnable(String name) {
thread = new Thread(this, name);
thread.start();
}
Summary
In this exercise, you have learned how to create a class that implements Runnable
interface and starts a thread.
}
}
Code-3.11: ThreadGroupTest.java
5. For your own exercise, do the following. Build and run the application.
• Modify ThreadGroupTest.java to create another (4th) SimpleThread instance
using your capital city of your country.
return list;
}
}
Code-3.21: DisplayAllThreads.java
}
}
Code-3.31: ThreadsPriority.java
Summary
In this exercise, you have learned how to retrieve information on a ThreadGroup.
Exercise 4: Synchronization
In this exercise, you are going to exercise how to do synchronization among threads.
1. Build and run a program in which threads are NOT synchronized
In this step, you are going to build an application that displays a result that is not desirable since
threads are not synchronized.
}
Code-4.11: UnsynchronizedExample.java
Thread thread;
String str1, str2;
}
Code-4.12: PrintStringsThread.java
4. Write TwoStrings.java as shown in Code-4.13 below. Study the code by paying special
attention to the bold fonted parts. Note that the print method is not synchronized.
(4.2) Build and run a program in which threads are synchronized through
synchronized method
In this step, you are going to build an application that displays a desired result because the
threads are synchronized.
}
Code-4.21: SynchronizedExample1.java
Thread thread;
String str1, str2;
}
Code-4.22: PrintStringsThread.java
4. Write TwoStrings.java as shown in Code-4.23 below. Study the code by paying special
attention to the bold fonted parts.
(4.3) Build and run a program in which threads are synchronized through
synchronized statement on common object
In this step, you are going to build another application that displays a desired result because the
threads are synchronized.
}
Code-4.31: SynchronizedExample2.java
3. Write PrintStringsThread.java as shown in Code-4.32 below. Study the code by paying special
attention to the bold fonted parts.
Thread thread;
String str1, str2;
TwoStrings ts;
Summary
In this exercise, you have learned how to use synchronization.
3. Write CubbyHole.java as shown in Code-5.12 below. Study the code by paying special
attention to the bold fonted parts.
// Unsynchronized CubbyHole.
//
// Results are unpredictable; a number may be read before a number has
// been produced or multiple numbers may be produced with only one or
// two being read adding synchronization ensures that a number is first
// produced, then read in the correct order.
p1.start();
c1.start();
}
}
Code-5.21: ProducerConsumerSynchronized.java
3. Write CubbyHole.java as shown in Code-5.22 below. Study the code by paying special
attention to the bold fonted parts.
Summary
In this exercise, you have learned how to perform inter-thread commmunication by the usage of
wait(), notify(), and notifyAll() methods.
In this exercise, you will learn how to use Timer and TimerTask to schedule a single or repeating
task.
1. Schedule one-time task
2. Schedule repeating task
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Toolkit;
/**
* Schedule a task that executes once every second.
* Beep every second.
*/
public AnnoyingBeep() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(),
0, //initial delay
1*1000); //subsequent rate
}
In this exercise, you have learned how to use Timer and TimerTask classes to schedule one-time
or repeating tasks.
Homework exercise
1. The homework is to create a new NetBeans project called MyRunnableProject as following.
• Create a class called MyCurrentDate that implements Runnable interface.
• The MyCurrentDate class displays the current date and time 10 times, with 100 milli
seconds interval - use sleep() method for this interval.
• Create a class called MyMain, which contans main() method, in which 3 instances of
MyCurrentDate threads are being run.
2. Hand in the Assignment.