Threads in Java

Threads in Java

exploreJava logo

In programming, there will be a need to execute more than one program concurrently. In Java, concurrent execution of the program is done with the concept of Thread. The main method is run by the main Thread. We can create any number of threads for execution. In Java, there are two different ways to create a thread:

  • Extending Thread class
  • Implementing Runnable Interface

Extending Thread class

A thread can be created by extending a Thread superclass. The class which extends Thread overrides the run() method, the entry point for the new thread. A call to start() method begins execution of the new thread.

A class will extend Thread class and overrides run() method (run() method is defined inside the Thread class)). A class extending Thread class can define its methods and variables.

class A extends Thread{
// other statements
public void run(){
//statements
}
}

In the given program a thread class is created by extending the Thread class. In that class integer array of size 5 is used inside the constructor, a method to initialize the array is defined, and the run method is defined. In the side, run method values stored in the array are checked for division by 23. Inside the run, the sleep() method of the Thread class is called; the sleep method stops the execution of code for the specified period of time; 1000 is equal to 1 second.

class A extends Thread{
int[] arr;
int i;
String threadName;
public A(String threadName){
arr = new int[5];
this.threadName = threadName;
}
public void setArr(int[] arr){
this.arr = arr;
}
public void run(){
for(i=0;i<arr.length;i++){
if(arr[i]%23 ==0)
System.out.println("Inside "+threadName+": "+arr[i] +" is OKI.");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){}
}
}
}

class ThreadDemo { public static void main(String[] arrr){ A a = new A("A"); A b = new A("B"); int[] arr = {23,33,46,69,92}; int[] arr1 = {23,33,34,115,52}; a.setArr(arr); a.start(); b.setArr(arr1); b.start(); } } Output: Inside B: 23 is OKI. Inside A: 23 is OKI. Inside A: 46 is OKI. Inside A: 69 is OKI. Inside B: 115 is OKI.
Inside A: 92 is OKI.

Creating Thread by implementing Runnable Interface

A Thread can be created on any object that implements Runnable interface. Runnable interface abstracts a unit of executable code. A class implementing the runnable interface has to define a single method called run(). Inside run() method, a code that forms a new thread is defined; run() can call other methods, use other classes, declare variables. The run() method provides the entry point for another, concurrent thread for execution within the program. The thread will end as the run() method returns.

In the class implementing Runnable interface, an object of Thread class is created. Thread class has different constructors. The new thread object will start only when start() method of the Thread object is called; start() initiates a call to run().

class aThread implements Runnable{
Thread t;
String name;
public aThread(String name){
this.name = name;
t = new Thread(this,name);
}
public void run(){
for(int i=0;i<name.length();i++){
System.out.print(name.charAt(i)+", ");
try{
Thread.sleep(500);
}catch(InterruptedException ie){}
}
}
}

class runnableThread{ public static void main(String[] aa){ aThread thread1 = new aThread("Running"); thread1.t.start(); } } Output:
R, u, n, n, i, n, g,


Java Swing V/S Java AWT

Member Access and Inheritance in Java

Post a Comment

Previous Post Next Post