- public class StartFromMethod {
- private Thread t;
- private int number;
- private int count = 1;
-
- public StartFromMethod(int number) {
- this.number = number;
- }
-
- public void runTask() {
- if (t == null) {
- t = new Thread() {
- public void run() {
- while (true) {
- System.out.println("Thread-" + number + " run " + count
- + " time(s)");
- if (++count == 3)
- return;
- }
- }
- };
- t.start();
- }
- }
-
- public static void main(String[] args) {
- for (int i = 0; i < 5; i++)
- new StartFromMethod(i).runTask();
- }
- }
复制代码 |