You can create java threads in 2 ways: • Define a class that implements the Runn
ID: 3814866 • Letter: Y
Question
You can create java threads in 2 ways:
• Define a class that implements the Runnable interface. Create an object of this class and pass it as an argument to a Thread constructor. Then, use the start() method to start executing the thread.
• Subclass Thread and override its run() method. Then, use the start() method to start executing the thread. We will demonstrate this method.
Open an editor (vi or gedit) and save the program below as HelloRunnable.java.
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {//main thread
(new Thread(new HelloRunnable())).start();
}
}
Compile the program:
student@Ubuntu:~/labs/lab2$ javac HelloRunnable.java
Run the program:
student@Ubuntu:~/labs/lab2$ java HelloRunnable
What is the output of this program?
he method Thread.sleep() causes the current thread to suspend execution for a specified period (in ms). This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.
In Java, an interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.
When a thread interrupts the a thread while it is sleeping, an InterruptedException exception is thrown. As a programmer, you can catch it and respond to it:
Open an editor (vi or gedit) and save the program below as SleepyThread.java.
public class SleepyThread extends Thread {
public void run() {
System.out.println("New thread: Going to sleep for 2s!");
try{
Thread.sleep(2000);
System.out.println("New thread: That was a good sleep!");
}catch(InterruptedException e){
System.out.println("New thread: Hmmmm?!! What????");
}
}
public static void main(String args[]) {
//Sleepy thread is started
SleepyThread st = new SleepyThread();
st.start();
//Main thread sleeps for 1s.
try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println("Main thread: Interruptor was interrupted!");
}
//Main thread interrupts st
System.out.println("Main thread: Interrupting new thread!");
try{
st.interrupt();
}catch(Exception e){
System.out.println("Main thread: Exception ! "+e);
}
}
}
Compile the program:
student@Ubuntu:~/labs/lab2$ javac SleepyThread.java
Run the program:
student@Ubuntu:~/labs/lab2$ java SleepyThread
What is the output of this program?
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {//main thread
(new Thread(new HelloRunnable())).start();
}
}
Explanation / Answer
The basic concept behind threads is to execute a task/method as a separate process without disturbing the main proccess. By this we can create several different independent processes to perform different tasks parallely within a single program.
The below class implements the Runnable interface. After creating an object of this class and pass it as an argument to a Thread constructor, start() method to start executing the thread.
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {//main thread
(new Thread(new HelloRunnable())).start();
}
}
Output: Hello from a thread!
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
The below class extends Thread class, which makes this class to be a subclass of Thread class. And override its run() method. Then, use the start() method to start executing the thread.
The method Thread.sleep() causes the current thread/process to suspend execution for a specified period (in ms) of time. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system.
In Java, an interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.
When a thread interrupts the a thread while it is sleeping, an InterruptedException exception is thrown. As a programmer, you can catch it and respond to it:
public class SleepyThread extends Thread {
public void run() {
System.out.println("New thread: Going to sleep for 2s!");
try{
Thread.sleep(2000);
System.out.println("New thread: That was a good sleep!");
}catch(InterruptedException e){
System.out.println("New thread: Hmmmm?!! What????");
}
}
public static void main(String args[]) {
//Sleepy thread is started
SleepyThread st = new SleepyThread();
st.start();
//Main thread sleeps for 1s.
try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println("Main thread: Interruptor was interrupted!");
}
//Main thread interrupts st
System.out.println("Main thread: Interrupting new thread!");
try{
st.interrupt();
}catch(Exception e){
System.out.println("Main thread: Exception ! "+e);
}
}
}
Output:
New thread: Going to sleep for 2s!
<the below output appears after 2 seconds>
Main thread: Interrupting new thread!
New thread: Hmmmm?!! What????
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.