
Java threads ⬆️
Threads are Java class that makes a program run efficiently and do multiple tasks at the same time without interrupting the main program in the background.
Threads are simply Java class that promotes efficient multi-tasking of a program at same time.

Types of Java threads
There are two types of threads in Java:
- Extending Thread class
- Implementing Runnable interface
Extending Thread class
If a class extends to the Thread class the thread can be run by creating an instance of class and call it’s start()
method: see code⬇️
package myThreads;
public class Main extends Thread/* Here Main class extended to Thread class */{
public static void main(String[] args) {
// write your code here
Thread thread = new Thread();// here the Thread class object created
thread.start();// here the start() method is called
System.out.println("I am running outside the thread.");// this runs outside the thread
}
public void run(){
System.out.println("I am running inside the thread.");// this runs inside the thread
}
}}
Implementing Runnable interface
If a class implements the Runnable
interface, the thread can be run by passing an instance of the class to a thread
object’s start()
method: see code⬇️
package myThreads2;
public class Main implements Runnable{
public static void main(String[] args) {
// write your code here
Main obj = new Main();
Thread thread = new Thread(obj);
thread.start();
System.out.println("I am running outside Runnable interface");
}
public void run(){
System.out.println("I am running inside Runnable interface");
}
}
Extending threads vs. implementing Runnable interface
Extending threads:
A class extended to the thread
class can’t be extended to any other class.
Implementing Runnable interface:
It is possible to extend from another class after implementing the Runnable interface. i.e. myClass extend otherClass implements Runnable {}
package myThreads2;
public class myThreads2a {
public static String aWord = "Java";
}
//this is a separate class the Main class extended from.--------------------------------------------------------------------
//This is the main class.package myThreads2;
public class Main extends myThreads2a implements Runnable{
public static void main(String[] args) {
// write your code here
System.out.println(aWord);
Main obj = new Main();
Thread thread = new Thread(obj);
thread.start();
System.out.println("I am running outside Runnable interface");
}
public void run(){
System.out.println("I am running inside Runnable interface");
}
}
Concurrency problems
When the threads and main program are reading and writing the same variables the values are unpredictable. This problem occurrence is called concurrency.
package myThreads;
public class Main extends Thread/* Here Main class extended to Thread class */{
static int x = 5;
public static void main(String[] args) {
// write your code here
Thread thread = new Thread();// here the Thread class object created
thread.start();// here the start() method is called
System.out.println(x);// Boss2
x++;
System.out.println(x);// Boss3
}
public void run(){
//x--;
System.out.println("Boss1"+ x--);// Boss1
}
}
// here there is conflict between these bosses
Conquering concurrency
To avoid concurrency problems it is best to share as few attributes between threads as possible. If attributes need to be shared, one possible solution is to use the isAlive()
method of the thread to check whether the thread has finished running before using any attributes that the thread can change. ex.⬇️
package myThreads;
public class Main extends Thread/* Here Main class extended to Thread class */{
public static int x = 5;
public static void main(String[] args) {
// write your code here
Thread thread = new Thread();// here the Thread class object created
thread.start();// here the start() method is called
while (thread.isAlive()){
System.out.println("Loading......");
}
System.out.println(x);// Boss2
x++;
System.out.println(x);// Boss3
}
public void run(){
x++;
}
}
// here there is conflict between these bosses is solved by //displaying loading when conflict still occurs.
To learn more about Java click me
👋