0% found this document useful (0 votes)
7 views

EEI4362_LAB3_PS_2023_2024

This document outlines a practical lab session for a Bachelor of Software Engineering course focusing on Object Oriented Design, specifically on Java threading. It covers key concepts such as thread creation, lifecycle, and methods, along with several code examples demonstrating these principles. Additionally, it includes exercises comparing different threading implementations and synchronization techniques.

Uploaded by

Iam Binesha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

EEI4362_LAB3_PS_2023_2024

This document outlines a practical lab session for a Bachelor of Software Engineering course focusing on Object Oriented Design, specifically on Java threading. It covers key concepts such as thread creation, lifecycle, and methods, along with several code examples demonstrating these principles. Additionally, it includes exercises comparing different threading implementations and synchronization techniques.

Uploaded by

Iam Binesha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

THE OPEN UNIVERSITY OF SRI LANKA

DEPARTMENT OF ELECTRICAL & COMPUTER ENGINEERING


BACHELOR OF SOFTWARE ENGINEERING
EEI/EEX4362 - OBJECT ORIENTED DESIGN
LAB 3 PRACTICAL SESSION

After completing this lab session student should be able to:


 Explain the use of threads in Java in an application.
 Use Java thread methods appropriately in an application.
 Explain the four states in thread life cycle in Java.
 Create threads by extending Thread class and implementing Runnable interface.

 Discuss the use of threads, existing java thread methods and thread life cycle in Java.
 Run the programs given inside boxes.
 Java Thread Example 1 - extending Thread class.

class Multi extends Thread{


public void run(){
System.out.println("thread is running...");

}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
 Java Thread Example 2- implementing Runnable interface.

class TestwithRunnable implements Runnable{


public void run(){
System.out.println("thread is running...");
}

public static void main(String args[]){


TestwithRunnable m1=new TestwithRunnable();
Thread t1 =new Thread(m1);
t1.start();
}
}

 Briefly discuss the need for creating Thread class object explicitly

Page 1 of 7
 Java Thread example 3 - sleep method in java

class TestSleepMethod1 extends Thread{


public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedExcep
tion e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();

t1.start();
t2.start();
}
}

 Discuss what happens if we keep a thread sleep for the specified time
 Java Thread example 4 - trying to start a thread twice

public class TestThreadTwice1 extends Thread{


public void run(){
System.out.println("running...");
}
public static void main(String args[]){
TestThreadTwice1 t1=new TestThreadTwice1();
t1.start();
t1.start();
}
}

 Discuss what happens if we start a thread twice

Page 2 of 7
Exercise 01
Compare the two programs given as A and B

Program A

class TestCallRun1 extends Thread{


public void run(){
System.out.println("running...");
}
public static void main(String args[]){
TestCallRun1 t1=new TestCallRun1();
t1.run }
}

Program B

class TestCallRun2 extends Thread{


public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e){
System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestCallRun2 t1=new TestCallRun2();
TestCallRun2 t2=new TestCallRun2();

t1.run();
t2.run();
}
}

Exercise 2
Compare the two programs given as C and D

Page 3 of 7
Program C

class TestJoinMethod1 extends Thread{


public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod1 t1=new TestJoinMethod1();
TestJoinMethod1 t2=new TestJoinMethod1();
TestJoinMethod1 t3=new TestJoinMethod1();
t1.start();
try{
t1.join();
}catch(Exception e){System.out.println(e);}

t2.start();
t3.start();
}
}

Program D
class TestJoinMethod2 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod2 t1=new TestJoinMethod2();
TestJoinMethod2 t2=new TestJoinMethod2();
TestJoinMethod2 t3=new TestJoinMethod2();
t1.start();
try{
t1.join(1500);
}catch(Exception e){System.out.println(e);}

t2.start();
t3.start();
}
}

Page 4 of 7
 Java Thread example 5- Thread Pool
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class WorkerThread implements Runnable {
private String message;
public WorkerThread(String s){
this.message=s;
}
public void run() {
System.out.println(Thread.currentThread().getName()+" (Start) message = "+message);
Processmessage
System.out.println(Thread.currentThread().getName()+" (End)");
}
private void processmessage() {
try { Thread.sleep(2000); }
catch (InterruptedException e) {
e.printStackTrace(); }
}
}

public class TestThreadPool {


public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
Runnable worker = new WorkerThread("" + i);
executor.execute(worker);
}
executor.shutdown();
while (!executor.isTerminated()) { }

System.out.println("Finished all threads");


}
}

Exercise 3
Compare the two programs given as E and F, with and without synchronized
methods.

Page 5 of 7
Program E
class Table{
void printTable(int n){
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
} } }
class MyThread1 extends Thread{
Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
} }
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
} }

class TestSynchronization1{
public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}

Page 6 of 7
Program F

class Table{
synchronized void printTable(int n){
for(int i=1;i<=5;i++){
System.out.println(n*i);
try{
Thread.sleep(400);
}catch(Exception e){System.out.println(e);}
}

}
}

class MyThread1 extends Thread{


Table t;
MyThread1(Table t){
this.t=t;
}
public void run(){
t.printTable(5);
}

}
class MyThread2 extends Thread{
Table t;
MyThread2(Table t){
this.t=t;
}
public void run(){
t.printTable(100);
}
}

public class TestSynchronization2{


public static void main(String args[]){
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
}

Try all the examples given in https://www.javatpoint.com/multithreading-in-java

Page 7 of 7

You might also like