TestRunnable.java 599 B

123456789101112131415161718192021222324252627
  1. public class TestRunnable {
  2. public static void main(String[] args) {
  3. MyRunnable runnable = new MyRunnable();
  4. Thread thread = new Thread(runnable);
  5. thread.start();
  6. }
  7. }
  8. class MyRunnable implements Runnable {
  9. int id = -1;
  10. public MyRunnable() {}
  11. public MyRunnable(int id) {
  12. this.id = id;
  13. }
  14. @Override
  15. public void run() {
  16. try {
  17. Thread.sleep(1000);
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. System.out.println(id == -1 ? "Finished." : ("Thread " + id + " finished."));
  22. }
  23. }