Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java initialize object array 
Java :: check if a char is a space java 
Java :: java singleton 
Java :: hashmap in java 
Java :: list of string to string array in java 
Java :: tableau de chaîne de caractère en java 
Java :: actuator spring boot 
Java :: declare a hashmap in java 
Java :: minimum and maximum in array in java 
Java :: how to create xml file in java 
Java :: java sort a map by keys 
Java :: java not equal to 
Java :: android bottom navigation hiding views 
Java :: Compare integers java sort 
Java :: android iterate through radio group java 
Java :: how to create object of abstract class in java 
Java :: dropdown show hide div event in jsp 
Java :: check if optional is empty java 
Java :: java get substring 
Java :: Remove an element at a specific index from an array in Java 
Java :: java continue keyword 
Java :: How to merge two sorted arrays in Java? 
Java :: maximum integer in array java 
Java :: how to pass a float between activities in android studio 
Java :: add infinite numbers to variable java 
Java :: default case in Java switch-case 
Java :: isnumeric matches in java 
Java :: Read File and Resource in JUnit Test into Stream 
Java :: array.swap java 
Java :: java sub function 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =