[go: up one dir, main page]

0% found this document useful (0 votes)
58 views10 pages

Thread Methods

The Java Thread Methods document describes 12 key methods of the Thread class in Java: 1. start() - Starts the execution of the thread and calls its run() method. 2. run() - The main method that is executed when a thread runs. 3. sleep() - Blocks the currently running thread for a specified time period. 4. currentThread() - Returns a reference to the currently running thread. 5. join() - Blocks the calling thread until another thread finishes execution. 6. getPriority() - Returns the priority of the thread. 7. setPriority() - Changes the priority of the thread. 8. getName() - Returns the name of the thread

Uploaded by

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

Thread Methods

The Java Thread Methods document describes 12 key methods of the Thread class in Java: 1. start() - Starts the execution of the thread and calls its run() method. 2. run() - The main method that is executed when a thread runs. 3. sleep() - Blocks the currently running thread for a specified time period. 4. currentThread() - Returns a reference to the currently running thread. 5. join() - Blocks the calling thread until another thread finishes execution. 6. getPriority() - Returns the priority of the thread. 7. setPriority() - Changes the priority of the thread. 8. getName() - Returns the name of the thread

Uploaded by

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

Java Thread Methods

These are the methods that are available in the Thread class:

1. public void start()

It starts the execution of the thread and then calls the run() on this Thread object.

Example:

1
2 {
    public void run()
3     {
4         System.out.println("Thread is running...");
5     }
6     public static void main(String args[])
7     {
        StartExp1 thread1=new StartExp1();
8         thread1.start();
9     }
10 }
11
Output:

Thread is running…

2. public void run()

This thread is used to do an action for a thread. The run() method is instantiated if the thread
was constructed using a separate Runnable object.

Example:

1 public class RunExp1 implements Runnable


{
2
    public void run()
3     {
4         System.out.println("Thread is running...");
5     }
6     public static void main(String args[])
    {
7         RunExp1 r1=new RunExp1();
8         Thread thread1 =new Thread(r1);
9         thread1.start();
10     }
}
11
12
13
Output:

Thread is running…

3. public static void sleep()

This blocks the currently running thread for the specified amount of time.

Example:

1
2
3 public class SleepExp1 extends Thread
{
4     public void run()
5     {
6         for(int i=1;i<5;i++)
7         {
            try
8             {
9                 Thread.sleep(500);
10             }catch(InterruptedException e){System.out.println(e);}
11             System.out.println(i);
12         }
    }
13     public static void main(String args[])
14     {
15         SleepExp1 thread1=new SleepExp1();
16         SleepExp1 thread2=new SleepExp1();
        thread1.start();
17
        thread2.start();
18     }
19 }
20
21
Output:

3
3

4. public static Thread currentThread()

It returns a reference to the currently running thread.

Example:

1
2 public class CurrentThreadExp extends Thread
3 {
4     public void run()
    {
5         System.out.println(Thread.currentThread().getName());
6     }
7     public static void main(String args[])
8     {
        CurrentThreadExp thread1=new CurrentThreadExp();
9
        CurrentThreadExp thread2=new CurrentThreadExp();
10         thread1.start();
11         thread2.start();
12     }
13 }
14
Output:

Thread-0

Thread-1

5. public void join()

It causes the current thread to block until the second thread terminates or the specified
amount of milliseconds passes.

Example:

1 public class JoinExample1 extends Thread


{
2
    public void run()
3     {
4         for(int i=1; i<=4; i++)
5         {
6
7
8             try
9             {
10                 Thread.sleep(500);
            }catch(Exception e){System.out.println(e);}
11
            System.out.println(i);
12         }
13     }
14     public static void main(String args[])
15     {
        JoinExample1 thread1 = new JoinExample1();
16         JoinExample1 thread2 = new JoinExample1();
17         JoinExample1 thread3 = new JoinExample1();
18         thread1.start();
19        try
20         {
        thread1.join();
21         }catch(Exception e){System.out.println(e);}
22         thread2.start();
23         thread3.start();
24     }
}
25
26
27
Output:

4
4

6. public final int getPriority()

It is used to check the priority of the thread. When a thread is created, some priority is
assigned to it. This priority is assigned either by the JVM or by the programmer explicitly
while creating the thread.

Example:

1
2 public class JavaGetPriorityExp extends Thread
3 {
4     public void run()
5     {
        System.out.println("running thread name is:"+Thread.currentThread().getName()
6     }
7     public static void main(String args[])
8     {
9         JavaGetPriorityExp t1 = new JavaGetPriorityExp();
        JavaGetPriorityExp t2 = new JavaGetPriorityExp();
10
        System.out.println("t1 thread priority : " + t1.getPriority());
11         System.out.println("t2 thread priority : " + t2.getPriority());
12         t1.start();
13         t2.start();
14     }
}
15
16
Output:

t1 thread priority : 5

t2 thread priority : 5

running thread name is:Thread-0

running thread name is:Thread-1

7. public final void setPriority()

This method is used to change the priority of the thread. The priority of every thread is
represented by the integer number from 1 to 10. The default priority of a thread is 5.

Example:

1 public class JavaSetPriorityExp1 extends Thread


2
3 {
4     public void run()
    {
5         System.out.println("Priority of thread is: "+Thread.currentThread().getPriori
6     }
7     public static void main(String args[])
8     {
        JavaSetPriorityExp1 t1=new JavaSetPriorityExp1();
9
        t1.setPriority(Thread.MAX_PRIORITY);
10         t1.start();
11     }
12 }
13
Output:

Priority of thread is: 10

8. public final String getName()

This method of thread class is used to return the name of the thread. We cannot override this
method in our program, as this method is final.

Example:

1
2 public class GetNameExample extends Thread
3 {
4     public void run()
5     {
        System.out.println("Thread is running...");
6     }
7     public static void main(String args[])
8     {
9         // creating two threads
10         GetNameExample thread1=new GetNameExample();
        GetNameExample thread2=new GetNameExample();
11         System.out.println("Name of thread1: "+ thread1.getName());
12         System.out.println("Name of thread2: "+thread2.getName());
13         thread1.start();
14         thread2.start();
    }
15
}
16
17
Output:

Name of thread1: Thread-0

Name of thread2: Thread-1


Thread is running…

Thread is running…

9. public final void setName()

This method changes the name of the thread.

Example:

1
2 public class SetNameExample extends Thread
3 {
4     public void run()
5     {
6         System.out.println("running...");
    }
7     public static void main(String args[])
8     {
9         SetNameExample thread1=new SetNameExample();
10         SetNameExample thread2=new SetNameExample();
        thread1.start();
11
        thread2.start();
12         thread1.setName("Kadamb Sachdeva");
13         thread2.setName("Great learning");
14         System.out.println("After changing name of thread1: "+thread1.getName());
15         System.out.println("After changing name of thread2: "+thread2.getName());
    }
16 }
17
18
Output:

After changing name of thread1: Kadamb Sachdeva

After changing name of thread2: Great Learning

running…

running…

10. public long getId()

It returns the identifier of the thread. The thread ID is a number generated when the thread
was created. This ID cannot be changed during its lifetime. But when the thread is
terminated, the ID can be reused.
Example:

1
2 public class GetIdExample extends Thread
3 {
4     public void run()
    {
5         System.out.println("running...");
6     }
7     public static void main(String args[])
8     {
        GetIdExample thread1=new GetIdExample();
9
        System.out.println("Name of thread1: "+thread1.getName());
10         System.out.println("Id of thread1: "+thread1.getId());
11         thread1.start();
12     }
13 }
14
Output:

Name of thread1: Thread-0

Id of thread1: 21

running…

11. public final boolean isAlive()

This method checks if the thread is alive. A thread is in the alive state if the start() method of
thread class has been called and the thread has not yet died.

Example:

1 public class JavaIsAliveExp extends Thread


{
2
    public void run()
3     {
4         try
5         {
6             Thread.sleep(300);
            System.out.println("is run() method isAlive "+Thread.currentThread().isAl
7         }
8         catch (InterruptedException ie) {
9         }
10     }
11     public static void main(String[] args)
    {
12         JavaIsAliveExp thread1 = new JavaIsAliveExp();
13         System.out.println("before starting thread isAlive: "+thread1.isAlive());
14         thread1.start();
15         System.out.println("after starting thread isAlive: "+thread1.isAlive());
16
17
    }
18 }
19
20
Output:

before starting thread isAlive: false

after starting thread isAlive: true

is run() method isAlive true

12. public static void yield()

This method pauses the execution of the current thread to execute other threads temporarily.

Example:

1
2
public class JavaYieldExp extends Thread
3 {
4     public void run()
5     {
6         for (int i=0; i<3 ; i++)
7             System.out.println(Thread.currentThread().getName() + " in control");
    }
8     public static void main(String[]args)
9     {
10         JavaYieldExp thread1 = new JavaYieldExp();
11         JavaYieldExp thread2 = new JavaYieldExp();
        thread1.start();
12
        thread2.start();
13         for (int i=0; i<3; i++)
14         {
15             thread1.yield();
16             System.out.println(Thread.currentThread().getName() + " in control");
        }
17     }
18 }
19
20
Output:

main in control

main in control
main in control

Thread-0 in control

Thread-0 in control

Thread-0 in control

Thread-1 in control

Thread-1 in control

Thread-1 in control

You might also like