DEV Community

Saravanan B
Saravanan B

Posted on

Core Java - MultiThreading - 1

Single Thread -- In this example the program executes by order.

public class SingleThread {
    public static void main(String[] args) {
        SingleThread st = new SingleThread();
        st.printNumbers();
        for (int j = 0; j < 2; j++) {
            System.out.print("j: " + j + "\t");
        }
    }

    void printNumbers() {
        for (int i = 0; i < 2; i++) {
            System.out.print("i: " + i + "\t");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output -- i: 0 i: 1 j: 0 j: 1

MultiThread -- we can implement multithreading by extending Thread class or using runnable interface.

public class MultiThreaded extends Thread {
    public static void main(String[] args) {
        MultiThreaded mt = new MultiThreaded();
        mt.start();

        for (int j = 0; j < 5; j++) {
            System.out.print("j: " + j + "\t");
        }
    }

    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.print("i: " + i + "\t");
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Output --
Image description

2nd time output--

Image description

Sleep Method -- Thread.sleep(1000); If we use this in thread it will print the output taking the time mentioned in mille seconds. Above i mentioned 1000 which is equal to 1second.

Joiner -- wait for run method to execute first and then process the next line.

public class Joiner extends Thread {
    static int n;
    static int sum = 0;

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Sum of first 'N' numbers");
        System.out.println("Enter a vale");
        Scanner sc = new Scanner(System.in);
        Joiner.n = sc.nextInt();

        Joiner joiner = new Joiner();
        joiner.start();
        joiner.join();

        System.out.println("Sum of first "+Joiner.n+"  n numbers  is "+Joiner.sum);
    }

    public void run() {
        for(int i=1;i<= Joiner.n;i++) {
            Joiner.sum +=i;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Sum of first 'N' numbers
Enter a vale
2
Sum of first2 n numbers is 3

Output without Joiner --
Sum of first 'N' numbers
Enter a vale
2
Sum of first 2 n numbers is 0

Calculating Time taken for a thread.

long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
        System.out.println("total time taken "+(end-start)/1000+" seconds");
Enter fullscreen mode Exit fullscreen mode

Thread Identity - Assigne a name to Thread.

public class ThreadName extends Thread{
    public static void main(String[] args) {
        ThreadName t = new ThreadName();
        t.start();

        Thread current = Thread.currentThread();
        System.out.println("Current Thread name : "+current.getName());
    }

    public void run() {
        Thread t = Thread.currentThread();
        t.setName("Now Thread name is set to this");
        System.out.println("Thread name : "+t.getName());
    }
}
Enter fullscreen mode Exit fullscreen mode

Current Thread name : main
Thread name : Now Thread name is changed to this

thread priority -- In certain situation we need to set the tread to executed first.

Minimum is 1 and the maximum is 10.


public class ThreadName extends Thread {
    public static void main(String[] args) {

        ThreadName t1 = new ThreadName();
        t1.setPriority(MIN_PRIORITY);
        t1.setName("Minimun priortiy Thread");
        t1.start();

        ThreadName t = new ThreadName();
        t.setPriority(MAX_PRIORITY);
        t.setName("Maximum priority Thread");
        t.start();
    }

    public void run() {
        System.out.println("Thread name : " + Thread.currentThread().getName());
    }
}
Enter fullscreen mode Exit fullscreen mode

Output --

Thread name: Maximum priority Thread
Thread name: Minimum priority Thread

Here maximum priority thread is executed first and then minimum priority thread.

Its always up to JVM to which thread to get executed first even though if we set the priority.

Top comments (0)