fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Thread thread = new MyThread();
  14. thread.start();
  15. }
  16.  
  17. private static class MyThread extends Thread{
  18. private final static int runLoopCount = 10;
  19. private final static long runPeriod = 500;
  20. public void run(){
  21. int count = runLoopCount;
  22. while( count > 0){
  23.  
  24. //這邊先check something(不會有loop)
  25. boolean ok = doSomethings();
  26.  
  27. if( ok){
  28. return ;
  29. }
  30.  
  31. // 準備下一回合
  32. count--;
  33. try{
  34. Thread.sleep(runPeriod);
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39.  
  40. private boolean doSomethings(){
  41. Random random = new Random();
  42. int ok = random.nextInt(100);
  43. return (ok%2) == 0;
  44. }
  45. }
  46. }
  47.  
  48.  
  49.  
Success #stdin #stdout 0.1s 320896KB
stdin
Standard input is empty
stdout
Standard output is empty