A program consists of n thread printing 1 to 10. - Java Thread

Java examples for Thread:Thread

Description

A program consists of n thread printing 1 to 10.

Demo Code


public class Main {
    static int threadCount = 5;

    public static void main(String[] args) {

        for (int i = 1; i <= threadCount; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override//from ww w.j a v  a2s  .  c o m
                public void run() {
                    int thread = (int) Thread.currentThread().getId() - 10 + 1;
                    print(thread);
                }
            });
            thread.start();
        }
    }

    public static void print(int thread) {
        int temp = thread;
        int i = 1;
        while (temp <= 10) {
            System.out.println("Thread-" + thread + " : " + temp);
            temp = thread + threadCount * i++;
        }
    }
}

Related Tutorials