123456789101112131415161718192021222324252627 |
- public class TestRunnable {
- public static void main(String[] args) {
- MyRunnable runnable = new MyRunnable();
- Thread thread = new Thread(runnable);
- thread.start();
- }
- }
- class MyRunnable implements Runnable {
- int id = -1;
- public MyRunnable() {}
- public MyRunnable(int id) {
- this.id = id;
- }
- @Override
- public void run() {
- try {
- Thread.sleep(1000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println(id == -1 ? "Finished." : ("Thread " + id + " finished."));
- }
- }
|