Simple Multithread program

  1. import java.io.*;
  2. class patient implements Runnable
  3. {
  4. Thread t;
  5. String n;
  6. patient(String name)
  7. {
  8. n = name;
  9. t = new Thread(this);
  10. t.setName(name);
  11. t.start();
  12. }
  13. synchronized void attend()
  14. {
  15. try
  16. {
  17. System.out.println("Doctor is diagnosing the patient :" +n);
  18. Thread.sleep(1500);
  19. }
  20. catch(InterruptedException e)
  21. {
  22. System.out.println("Extension interrupted");
  23. }
  24.  
  25. }
  26. public void run()
  27. {
  28. attend();
  29. }
  30. }
  31. class muldemo
  32. {
  33. public static void main(String args[]) throws IOException
  34. {
  35. patient in[] = new patient[10];
  36. int count = 0;
  37. int w = 0;
  38. System.out.println("Hospital Simulation");
  39. try
  40. {
  41. do
  42. {
  43. String name = getstring();
  44. System.out.println("Name of the patient:"+name);
  45. System.out.println("Waiting time in mins :" +w);
  46. in[count] = new patient(name);
  47. Thread.sleep(2000);
  48. System.out.println("Two mins rest for doctor");
  49. w+=12;
  50. count++;
  51. }
  52. while(count<5);
  53. }
  54. catch(InterruptedException e)
  55. {
  56. System.out.println("Exception Interrupted");
  57. }
  58. }
  59. public static String getstring() throws IOException
  60. {
  61. InputStreamReader isr = new InputStreamReader(System.in);
  62. BufferedReader br = new BufferedReader(isr);
  63. String s = br.readLine();
  64. return(s);
  65. }
  66. }