1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import java.util.concurrent.Callable; import java.util.concurrent.FutureTask;
public class ImplementsCallable implements Callable<Integer>{ public static int i = 0;
public static void main(String[] args) { Thread.currentThread().setName("Thread main");
Callable<Integer> oneCallable = new ImplementsCallable(); FutureTask<Integer> oneTask = new FutureTask<>(oneCallable);
Thread oneThread = new Thread(oneTask); oneThread.start(); print();
}
@Override public Integer call() throws Exception { Thread.currentThread().setName("Thread in Callable"); System.out.println(Thread.currentThread().getName() + " i = " + i); return null; }
public static void print() { i=0; for (i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + ":" + i); } }
}
|