The concept of multithreading is similar to the concept of multitasking

Operating systems allows multiple processes to run on the same time like word and paint and calc

We want the same behavior in the scope of a single process/java application, we want to be able to launch multiple functions at the same time, this concept is called multithreading, and every bunch of code that runs in parallel with another is called to be run on a thread.

We can make a simple thread in only 3 steps

  1. Extending Thread class
  2. Overriding run method
  3. Call start method to start the thread

Simple Counter

Suppose we want to make a counter that counts from 0 to 100, and we want to be able to make two or more counters and launch them at the same time.

the simple code for this counter would be like this

 

for(int i=0; i<100; i++)
{
    System.out.println(i);
}

 

we would build a Counter and gives each counter a name to differentiate between them

 

public class Counter
{    
    private String id;
    private int countTo;

    public Counter(String id, int countTo)
    {
	this.id = id;
	this.countTo = countTo;
    }

    public void count()
    {
	for(int i=1; i

 

We want to make two counters

	Counter a = new Counter("A", 3);
	Counter b = new Counter("B", 2);

	a.count();
	b.count();

when invoking the previous code, the result would be

A | 1
A | 2
A | 3
B | 1
B | 2

This behavior is not acceptable, we want both counters to count at the same time.

To do that we should run these counters in different threads

Converting our counter to run on a thread

To convert our class to Thread, we should do three steps

//1 extend Thread class
public class Counter extends Thread

//2 Override run method and write the implementation inside it
    @Override
    public void run() {
	count();
    }

//3 call start
	Counter a = new Counter("A", 3);
	Counter b = new Counter("B", 2);

	a.start();
	b.start();

The result would be

B | 1
A | 1
B | 2
A | 2
A | 3

Alarm

we want to make something usefull

an alarm that notifies user after a specific time about a message

public class Alarm extends Thread {

    private int time;
    private String message;

    public Alarm(String message, int time) {
	this.message = message;
	this.time = time;
    }

    @Override
    public void run()
    {
	for(int i=time; i>0; i--)
	{
	    System.out.println(i + " seconds remaining for " + message);
	    try {
		Thread.sleep(1000);
	    } catch (InterruptedException ex) {
		ex.printStackTrace();
	    }
	}
	System.out.println("// Alarm | " + message);
    }

}

we can use it to make multiple concurrent alarms

	Alarm wakeup = new Alarm("Wake up", 2);
	Alarm breakfast = new Alarm("Breakfast", 4);
	Alarm leave = new Alarm("Leave house", 7);

	wakeup.start();
	breakfast.start();
	leave.start();

the result would be

2 seconds remaining for Wake up
7 seconds remaining for Leave house
4 seconds remaining for Breakfast
1 seconds remaining for Wake up
3 seconds remaining for Breakfast
6 seconds remaining for Leave house
2 seconds remaining for Breakfast
// Alarm | Wake up
5 seconds remaining for Leave house
1 seconds remaining for Breakfast
4 seconds remaining for Leave house
// Alarm | Breakfast
3 seconds remaining for Leave house
2 seconds remaining for Leave house
1 seconds remaining for Leave house
// Alarm | Leave house