I have to print the numbers between one to ten , with ten threads and they got to be consecutive numbers. I mean Thread0 - 10 print 1,1,1,1,1,1,1,1,1,1 then Thread 0-10 print 2,2,2,2,2,2,2,2,,2,2
class A extends Thread {
String name = "";
static Integer num = 1;
public void run() {
for(int i = 0 ; i < 10 ; i++) {
synchronized (num) {
num++;
System.out.println(name + " " + num);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
public static void main(String args[]) {
for (int i = 0; i < 10; i++) {
A t1 = new A();
t1.name = "Thread " + i;
t1.start();
}
}
I am trying this but it doesn't work
I tried to use the code above and one more attempt but i print the numbers from 1-10 but with one thread i mean Therad0 - 1,2,3,4,5,6,7,8,9,10 and Thread1 - 1,2,3,4,5,6,7,8,9,10