Answers for "print 1 to 10 using thread in java"

0

print 1 to 10 using thread in java

class MyRunnable implements Runnable {

    private static final int LIMIT = 20;
    private static volatile int counter = 0;
    private int id;

    public MyRunnable(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        outer:
        while(counter < LIMIT) {
            while (counter % NB_THREADS != id) {
                if(counter == LIMIT) break outer;
            }
            System.out.println("Thread "+Thread.currentThread().getName()+ " printed " + counter);
            counter += 1;
        }
    }
}
Posted by: Guest on October-20-2021

Code answers related to "print 1 to 10 using thread in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language