OnJava8-Examples/concurrency/SimpleThread.java

35 lines
836 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// concurrency/SimpleThread.java
2015-06-15 17:47:35 -07:00
// Inheriting directly from the Thread class.
public class SimpleThread extends Thread {
private int countDown = 5;
private static int threadCount = 0;
public SimpleThread() {
// Store the thread name:
super(Integer.toString(++threadCount));
start();
}
@Override
public String toString() {
return "#" + getName() + "(" + countDown + "), ";
}
@Override
public void run() {
while(true) {
System.out.print(this);
if(--countDown == 0)
return;
}
}
public static void main(String[] args) {
for(int i = 0; i < 5; i++)
new SimpleThread();
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
#4(5), #3(5), #5(5), #2(5), #2(4), #1(5), #4(4), #1(4),
#2(3), #2(2), #5(4), #3(4), #3(3), #3(2), #5(3), #5(2),
#2(1), #1(3), #4(3), #4(2), #1(2), #1(1), #5(1), #3(1),
#4(1),
2015-09-07 11:44:36 -06:00
*/