Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. public class busyWait {
  2. //Threads
  3. public static studentThread[] student;
  4. public static teacherThread teacher;
  5. public static timerThread timer;
  6.  
  7. //Default Values
  8. public static int capacity = 10;
  9. public static int num_Students = 15;
  10. public static int num_questions_A = 10;
  11. public static int num_questions_B = 10;
  12.  
  13. public static void main(String[] args){
  14.  
  15. //Command line inputs
  16. if(args.length > 0){
  17.  
  18. capacity = Integer.parseInt(args[0]);
  19. num_Students = Integer.parseInt(args[1]);
  20. num_questions_A = Integer.parseInt(args[2]);
  21. num_questions_B = Integer.parseInt(args[3]);
  22.  
  23. }
  24. /*
  25. * Initialize for timer, teacher and student.
  26. */
  27. timer = new timerThread();
  28. timer.start();
  29.  
  30. teacher = new teacherThread(timer);
  31. teacher.setCapacity(capacity);
  32. teacher.start();
  33.  
  34. student = new studentThread[num_Students];
  35.  
  36. for(int i=0; i<num_Students; i++){
  37.  
  38. student[i] = new studentThread(i+1, timer, teacher);
  39. student[i].setQuestionsA(num_questions_A);
  40. student[i].setQuestionsB(num_questions_B);
  41. student[i].setWillSleep(i*10);
  42. student[i].start();
  43.  
  44. }
  45. /*
  46. * A busy wait that guarantees no other hit for sleeps are
  47. * interrupted. Ends at exactly when all questions are
  48. * asked for question B, including its calculated random
  49. * time after which threads sleeping if they are in browsing mode
  50. * are interrupted.
  51. */
  52. while(timer.surfEnd()){
  53. //Busy wait
  54. }
  55. /*
  56. * Interrupts threads that are sleeping(browsing the net).
  57. */
  58. for(int i=0; i<num_Students; i++){
  59. if(student[i].getState().toString()=="TIMED_WAITING"){
  60. student[i].interrupt();
  61. }
  62.  
  63.  
  64. }
  65.  
  66. //Student threads calls join if it is still alive
  67. for(int i=0; i<num_Students; i++){
  68. if(student[i].isAlive()){
  69.  
  70. try {
  71. student[i].join();
  72. }
  73. catch (InterruptedException e){
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. }
  77. }
  78. }
  79.  
  80. System.out.println();
  81. System.out.println("Simulation Done.");
  82.  
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement