0% found this document useful (0 votes)
27 views2 pages

Multitreading Document

The code implements a Runnable interface to create a multithreaded program. It defines a run method with no implementation. In main, it creates a new Thread, calls its start method to execute, sleeps the thread for 1000ms, sets its priority to 1, gets and prints the priority, and prints that the thread is running.

Uploaded by

Treymax Sikan
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)
27 views2 pages

Multitreading Document

The code implements a Runnable interface to create a multithreaded program. It defines a run method with no implementation. In main, it creates a new Thread, calls its start method to execute, sleeps the thread for 1000ms, sets its priority to 1, gets and prints the priority, and prints that the thread is running.

Uploaded by

Treymax Sikan
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/ 2

Multithreading Code

package demotest;
public class thread_example1 implements Runnable {
@Override
public void run() {
}
public static void main(String[] args) {
Thread guruthread1 = new Thread();
guruthread1.start();
try {
guruthread1.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
guruthread1.setPriority(1);
int gurupriority = guruthread1.getPriority();
System.out.println(gurupriority);
System.out.println("Thread Running");
}
}

Explanation of the code:

Code Line 2: We are creating a class "thread_Example1" which is implementing


the Runnable interface (it should be implemented by any class whose instances
are intended to be executed by the thread.)

Code Line 4: It overrides run method of the runnable interface as it is mandatory


to override that method

Code Line 6: Here we have defined the main method in which we will start the
execution of the thread.

Code Line 7: Here we are creating a new thread name as "guruthread1" by


instantiating a new class of thread.

Code Line 8: we will use "start" method of the thread using "guruthread1"
instance. Here the thread will start executing.

Code Line 10: Here we are using the "sleep" method of the thread using
"guruthread1" instance. Hence, the thread will sleep for 1000 milliseconds.

Code 9-14: Here we have put sleep method in try catch block as there is
checked exception which occurs i.e. Interrupted exception.
Code Line 15: Here we are setting the priority of the thread to 1 from whichever
priority it was

Code Line 16: Here we are getting the priority of the thread using getPriority()

Code Line 17: Here we are printing the value fetched from getPriority

Code Line 18: Here we are writing a text that thread is running.

When you execute the above code, you get the following output:

You might also like