0% found this document useful (0 votes)
40 views7 pages

Lab 3 Sleep Method 456CSS-3

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)
40 views7 pages

Lab 3 Sleep Method 456CSS-3

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

BACHELOR OF SCIENCE IN COMPUTER SCIENCE

Lab 3
Sleep Method in Thread/ Multithread
456CSS-3
PARALLEL AND DISTRIBUTED SYSTEMS

Student name :- Hussien saad Alsalem


ID :- 442105077

Objective:

The objective of this lab is to

1) To demonstrate sleep method in thread

How to create thread

There are two ways to create a thread:


1. By extending Thread class
2. By implementing Runnable interface.
3.
Thread class:
Thread class provide constructors and methods to create and perform operations on a thread.
Thread class extends Object class and implements Runnable interface.
Commonly used Constructors of Thread class:
o Thread()
o Thread(String name)
o Thread(Runnable r)
o Thread(Runnable r,String name)
Commonly used methods of Thread class:
1. public void run(): is used to perform action for a thread.
2. public void start(): starts the execution of the thread.JVM calls the run() method on the
thread.
3. public void sleep(long miliseconds): Causes the currently executing thread to sleep
(temporarily cease execution) for the specified number of milliseconds.
4. public void join(): waits for a thread to die.
5. public void join(long miliseconds): waits for a thread to die for the specified
miliseconds.
6. public int getPriority(): returns the priority of the thread.
7. public int setPriority(int priority): changes the priority of the thread.
8. public String getName(): returns the name of the thread.
9. public void setName(String name): changes the name of the thread.
10. public Thread currentThread(): returns the reference of currently executing thread.
11. public int getId(): returns the id of the thread.
12. public Thread.State getState(): returns the state of the thread.
13. public boolean isAlive(): tests if the thread is alive.
14. public void yield(): causes the currently executing thread object to temporarily pause and
allow other threads to execute.
15. public void suspend(): is used to suspend the thread(depricated).
16. public void resume(): is used to resume the suspended thread(depricated).
17. public void stop(): is used to stop the thread(depricated).
18. public boolean isDaemon(): tests if the thread is a daemon thread.
19. public void setDaemon(boolean b): marks the thread as daemon or user thread.
20. public void interrupt(): interrupts the thread.
21. public boolean isInterrupted(): tests if the thread has been interrupted.
22. public static boolean interrupted(): tests if the current thread has been interrupted.
Runnable interface:

The Runnable interface should be implemented by any class whose instances are intended to be
executed by a thread. Runnable interface have only one method named run().
1. public void run(): is used to perform action for a thread.

Starting a thread:
start() method of Thread class is used to start a newly created thread. It performs following tasks:
o A new thread starts(with new callstack).
o The thread moves from New state to the Runnable state.
o When the thread gets a chance to execute, its target run() method will run.

1) Java Thread Example by 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();
}
}

Output: thread is running...

2) Java Thread Example by implementing Runnable interface

class Multi3 implements Runnable{


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

public static void main(String args[]){


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

Output:thread is running...
If you are not extending the Thread class, your class object would not be treated as a thread object.
So you need to explicitly create Thread class object. We are passing the object of your class
that implements Runnable so that your class run() method may execute.

Sleep method in java

The sleep() method of Thread class is used to sleep a thread for the specified amount of time.

Syntax of sleep() method in java

The Thread class provides two methods for sleeping a thread:


o public static void sleep(long miliseconds)throws InterruptedException
o public static void sleep(long miliseconds, int nanos)throws InterruptedException

Parameters
millis − This is the length of time to sleep in milliseconds.

Return Value
This method does not return any value.

Exception
InterruptedException − if any thread has interrupted the current thread. The interrupted status of
the current thread is cleared when this exception is thrown.

Example of sleep method in java


class TestSleepMethod1 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[]){
TestSleepMethod1 t1=new TestSleepMethod1();
TestSleepMethod1 t2=new TestSleepMethod1();

t1.start();
t2.start();
}
}
Output:
1
1
2
2
3
3
4
4
As you know well that at a time only one thread is executed. If you sleep a thread for the specified
time, the thread scheduler picks up another thread and so on.

/*
Pause Thread Using Sleep Method Example
This Java example shows how to pause currently running thread using
sleep method of Java Thread class.
*/

public class PauseThreadUsingSleep {

public static void main(String[] args) {

/*
* To pause execution of a thread, use
* void sleep(int milliseconds) method of Thread class.
*
* This is a static method and causes the suspension of the thread
* for specified period of time.
*
* Please note that, this method may throw InterruptedException.
*/

System.out.println ("Print number after pausing for 1000 milliseconds");


try{

for(int i=0; i< 5; i++){

System.out.println(i);

/*
* This thread will pause for 1000 milliseconds after
* printing each number.
*/
Thread.sleep(1000);
}
}
catch(InterruptedException ie){
System.out.println("Thread interrupted !" + ie);
}

}
}

/*
Output of this example would be
Print number after pausing for 1000 milliseconds
0
1
2
3
4
*/

You might also like