0% found this document useful (0 votes)
122 views11 pages

Object-Oriented Programming (CS F213) : BITS Pilani

This document discusses threads and the Thread class in Java. It describes the Thread class's important constructors and methods for manipulating threads. It also explains two ways to create threads: by extending the Thread class or implementing the Runnable interface. An example is provided that creates three GreetingThread objects with different messages, starts each thread, and has each print its message 10 times while sleeping for 100 milliseconds between prints.

Uploaded by

SAURABH MITTAL
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)
122 views11 pages

Object-Oriented Programming (CS F213) : BITS Pilani

This document discusses threads and the Thread class in Java. It describes the Thread class's important constructors and methods for manipulating threads. It also explains two ways to create threads: by extending the Thread class or implementing the Runnable interface. An example is provided that creates three GreetingThread objects with different messages, starts each thread, and has each print its message 10 times while sleeping for 100 milliseconds between prints.

Uploaded by

SAURABH MITTAL
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/ 11

Object-Oriented Programming (CS F213)

Module VI: Multithreading in Java


CS F213 RL 14.3: Thread Class in Java

BITS Pilani Dr. Pankaj Vyas


Department of Computer Science, BITS-Pilani, Pilani Campus
CS F213 RL 14.3 : Topics

Thread class in Java


How to Create Threads in Java

2 Object-Oriented Programming (CS F213)


Thread class in Java

Defined in java.lang package


Supplies Important Methods For Manipulating Threads
Important Constructors
1. Thread() Creates a Thread
2. Thread(Runnable r) Creates a Thread from a Runnable
Instance r [Note : Runnable is an Interface in Java]
3. Thread(String threadName) Creates a Thread with name
as threadName
4. Thread(Runnable r, String threadName) Creates a
Thread from a Runnable Instance r and assigns name as
threadName

3 Object-Oriented Programming (CS F213)


Thread Class (Important Method)
1. long getId() Returns the id of the Thread [Object Method]
2. String getName() Returns the name of the Thread [Object Method]
3. void setName(String name) Sets the name of the Thread [Object Method]
4. int getPriority() Returns the priority of the Thread [Object Method]
5. void setPriority(int priority) Sets the priority of the Thread [Object Method]
6. Thread.State getState() Returns the state of the Thread [Object Method]
7. boolean isAlive() Returns true if the Current Thread is Alive Otherwise false
[Object Method]
8. void run() This method defines the task that a thread has to do during its time
slice. [Note : If this thread was constructed using a separate Runnable run object,
then that Runnable object's run method is called; otherwise, this method does
nothing and returns.] [Object Method]
9. void sleep(long milliSeconds) Causes the current thread to sleep for mentioned
no of milliseconds [class Method]
10. void sleep(long milliSeconds, long nanoSeconds) Causes the current thread to
sleep for mentioned no of milliSeconds and nanoSeconds [ class Method]
11. void start() Used to start the thread.
12. void interrupt() Used to interrupt a Thread [Object Method]
13. boolean isInterrupted() Returns true if the current thread is interrupted
otherwise false [Object Method]

4 Object-Oriented Programming (CS F213)


How to Create a Thread?

Two Ways of Creating a Thread


By Extending a Thread class
By Implementing a Runnable Interface

5 Object-Oriented Programming (CS F213)


How to Create a Thread?
(By Extending a Thread class)
Step 1:Make a your thread class a sub class of the
Thread class
Step 2 : Override the run() method in the Thread sub
class
Example
class MyThread extends Thread
{
// Define Instance Fields
// Add Constructors as Required
// Add any other Method as Required
// Override run() Method
public void run()
{
// Provide the Code for run() Method
}// End of Method
}// End of class

6 Object-Oriented Programming (CS F213)


How to Create a Thread?
(By Implementing a Runnable Interface)
Java supports an interface named Runnable
Make a class implementing this Runnable interface

class MyThread implements Runnable


{
Runnable interface // Define Instance Fields
public interface Runnable // Add Constructors as Required
{ // Add any other Method as Required
public void run(); // Implement run() Method
} public void run()
{
// Provide the Code for run() Method
}// End of Method
}// End of class

7 Object-Oriented Programming (CS F213)


Creating and Staring Threads
(Example)
Create a Thread class named GreetingThread with two
instance fields as threadId:int and
greetingMessage:String. Add a suitable parameterized
constructor in the class. Supply a run() method which
prints out the greeting message on System.out ten
times. After printing out the greeting message the thread
sleeps for a period of 100 milliseconds. In the driver
code create three instances of the GreetingThread
class with values as {1,Hello Java}, {2,Java World},
{3,Welocme to Object-Oriented Programming} and start
all the threads.

8 Object-Oriented Programming (CS F213)


Creating and Staring Threads
(Example)
class GreetingThread extends Thread
{
private int threadId; // Thread id instance field
private String greetingMessage; // Greeting Message that Thread Displays
// Constructor Message
GreetingThread(int id, String msg)
{
threadId = id;
greetingMessage = msg;
}
// run() Method
public void run()
{
for(int i=0; i<10; i++)
{
System.out.println(Thread Id: + threadId + Message: + greetingMessage);
try
{
Thread.sleep(100);
}
catch(InteruptedException e) { }
}
}// End of Method
}// End of class

9 Object-Oriented Programming (CS F213)


Creating and Staring Threads
(Example )
class GreetingThread Test
{
public static void main(String args[])
{
// Creating Thread
GreetingThread t1 = new GreetingThread(1, Hello Java);
GreetingThread t2 = new GreetingThread(2, Java World);
GreetingThread t3 = new GreetingThread(3, Welcome to Object-Oriented Programming);

// Displaying the Priorities of Thread


System,out.println(t1.getPriority());
System,out.println(t2.getPriority());
System,out.println(t3.getPriority());

// Starting Threads
t1.start();
t2.start();
t3.start();

}// End of Method


}// End of class

10 Object-Oriented Programming (CS F213)


Thank You

11 Object-Oriented Programming (CS F213)

You might also like