Created
June 8, 2019 08:52
-
-
Save Arkango/c28a5cc0fd87e6ffcb711ccd84a8e036 to your computer and use it in GitHub Desktop.
Ten threads that are started together, each one print a sequence x ,x+1,x+2 ... where x is an int number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleThreadEx extends Thread { | |
private static int ThreadCounter = 0; | |
private int seriesCounter = 0; | |
private int lastNumber; | |
private int ThreadId; | |
public SimpleThreadEx(int value){ | |
ThreadId = ThreadCounter ++; | |
seriesCounter = value; | |
lastNumber = value +3; | |
System.out.println("Creando il Thread con id : "+ThreadId); | |
} | |
@Override | |
public void run(){ | |
while(true){ | |
System.out.println("ThreadId : "+ThreadId+" seriesNum : "+ ++seriesCounter); | |
if(seriesCounter > lastNumber){ | |
break; | |
} | |
} | |
} | |
} | |
public class ThreadExercise{ | |
public static void main(String ... args){ | |
int base = 5; | |
for(int i=0; i < 10; i++){ | |
new SimpleThreadEx(base).start(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment