Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package Hilos;
  2. public class Hilos {
  3. public void iniciar() {
  4. Ping x1= new Ping("ping", 1);
  5. Thread th1= new Thread(x1);
  6.  
  7. Ping x2= new Ping("ping",2);
  8. Thread th2= new Thread(x2);
  9.  
  10. Ping x3= new Ping("ping",3);
  11. Thread th3= new Thread(x3);
  12.  
  13. Ping x4= new Ping("ping",4);
  14. Thread th4= new Thread(x4);
  15.  
  16. Ping x5= new Ping("ping",5);
  17. Thread th5= new Thread(x5);
  18.  
  19. Ping x6= new Ping("pong",6);
  20. Thread th6= new Thread(x6);
  21.  
  22. Ping x7= new Ping("pong",7);
  23. Thread th7= new Thread(x7);
  24.  
  25. Ping x8= new Ping("pong",8);
  26. Thread th8= new Thread(x8);
  27.  
  28. Ping x9= new Ping("pong",9);
  29. Thread th9= new Thread(x9);
  30.  
  31. Ping x10= new Ping("pong",10);
  32. Thread th10= new Thread(x10);
  33.  
  34. th1.start();
  35. th2.start();
  36. th3.start();
  37. th4.start();
  38. th5.start();
  39. th6.start();
  40. th7.start();
  41. th8.start();
  42. th9.start();
  43. th10.start();
  44.  
  45. }
  46.  
  47. public static void main(String[] args) {
  48. Hilos h = new Hilos();
  49. h.iniciar();
  50. }
  51. }
  52.  
  53. class Ping implements Runnable {
  54. static boolean bandera = true;
  55. String c;
  56. int cual;
  57.  
  58. public Ping(String c, int cual) {
  59. this.c = c;
  60. this.cual = cual;
  61. }
  62.  
  63. public synchronized void imprimir1(){
  64. if(c=="ping"&&bandera==true){
  65. System.out.println(c+cual);
  66. bandera=false;
  67. }
  68. if(c.equals("pong")&&bandera==false){
  69. System.out.println("t"+c+cual);
  70. bandera=true;
  71. }
  72. }
  73.  
  74. @Override
  75. public void run() {
  76. while(true) {
  77. try {
  78. Thread.sleep(500);
  79. imprimir1();
  80. } catch (InterruptedException ex) {
  81. }
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement