You are on page 1of 5

Java Programming II-Lab1-Thread

A GUIDE TO ADVANCED JAVA


Module 1: Introduction to Threads Lab Guide for Lab1

Session Objectives
In this session, you will be practicing with Creating Threads Thread States Methods of Thread class Managing Threads Daemon Threads

Part 1 Getting started (30 minutes)


1. Create new thread class by extending the class Thread (15 minutes). The following program simulates a car racing using multi-threading technique. RaceCar extends Thread, therefore, it is runnable. In the Race class, an array of RaceCar objects is created and started asynchronously. Scan the code first, type the code, compile, run and observe the result. //RaceCar method extends Thread. Therefore, It is runnable. class RaceCar extends Thread { int finish; String name; RaceCar(int finish, String name){ this.finish = finish; this.name = name; } //Overide run() method for racing logic public void run(){ for (int i=0; i<finish; i++){ System.out.println(name + ": " + (i+1) + running); try { //sleep for a random amount of time. To get a random value, java.util.Math.random() method is used. Thread.sleep(Math.round(Math.random()*5000)); } catch(Exception e){} }

2009 FPT-Aptech

Page 1 / 5

Java Programming II-Lab1-Thread


System.out.println(name + " finished"); } }

public class Race { public static void main(String a[]){ //create an array of runnable RaceCar objects RaceCar[] cars = new RaceCar[5]; cars[0] = new RaceCar(10, "Mario"); cars[1] = new RaceCar(10, "Songuku"); cars[2] = new RaceCar(10, "Herman"); cars[3] = new RaceCar(10, "Doremon"); cars[4] = new RaceCar(10, "Hoang Phi Hong"); //start racing for(int i=0; i<5; i++) cars[i].start(); } } 2. Create new thread class by implementing the interface Runnable. (15 minutes) The following program demonstrates another technique for creating threads. Perform the same procedure for the program below: class Mosquito implements Runnable { private int id; public Mosquito(int id){ this.id = id; } // implement the run() method for flying logic public void run(){ if (id == 12) { try { Thread.sleep(10000); } catch(Exception e){} } for (int i=0; i<10; i++){ System.out.println("The mosquito " + id + is running); }

} } //A test class for Mosquito public class RunnableMosquitoTest{

2009 FPT-Aptech

Page 2 / 5

Java Programming II-Lab1-Thread


public static void main(String args[]){ //create some mosquitos Mosquito m1 = new Mosquito(12); Mosquito m2 = new Mosquito(21); Mosquito m3 = new Mosquito(22); Mosquito m4 = new Mosquito(32); //let them fly Thread t1 = new Thread(m1); t1.start(); Thread t2 = new Thread(m2); t2.start(); System.out.println(t1.getName()); System.out.println(t2.getName()); //count the number of flying mosquitos System.out.println("No. of Thread: " + Thread.activeCount()); } }

Part 2 Workshops (30 minutes)



Quickly look at Module 1s workshops for reviewing basic steps for creating and managing threads. Try to compile, run and observe the output of sample code provided for related workshop. Discuss with your class-mate and your instructor if needed.

Part 3 Lab Assignment (60 minutes)


Do the assignment for Module 1 carefully. Discuss with your class-mates and your instructor if
needed.

Part 4 Do it your self


1.1. Create a Thread named MyThread (by extending from the class Thread or by implementing the interface Runnable) does these tasks: - Display name of the current thread. - Rename the created thread to myJavaThread and display this name. - Overwrite the method run() by displaying first 10 even numer. Delay time between each display time is 1500 ms.

2009 FPT-Aptech

Page 3 / 5

Java Programming II-Lab1-Thread 1.2. Write a program to create the ouput as following figure:

Create three threads and the main thread. Execute each thread as simultaneous tasks. Display information when exiting each thread. 1.3. Create a new thread class that prints a value in duration. Information about a thread includes: message, duration, priority.
MESSAGE BOARD ==================== Number of messages: 2 Message 1: multithreading Timeout: 1000 Priority: high Message 2: multitasking Timeout: 2000 Priority: medium Result: Multithreading Multithreading Multitasking

2009 FPT-Aptech

Page 4 / 5

Java Programming II-Lab1-Thread


Multithreading Multithreading Multitasking ..

2009 FPT-Aptech

Page 5 / 5

You might also like