Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Hilos;
- public class Hilos {
- public void iniciar() {
- Ping x1= new Ping("ping", 1);
- Thread th1= new Thread(x1);
- Ping x2= new Ping("ping",2);
- Thread th2= new Thread(x2);
- Ping x3= new Ping("ping",3);
- Thread th3= new Thread(x3);
- Ping x4= new Ping("ping",4);
- Thread th4= new Thread(x4);
- Ping x5= new Ping("ping",5);
- Thread th5= new Thread(x5);
- Ping x6= new Ping("pong",6);
- Thread th6= new Thread(x6);
- Ping x7= new Ping("pong",7);
- Thread th7= new Thread(x7);
- Ping x8= new Ping("pong",8);
- Thread th8= new Thread(x8);
- Ping x9= new Ping("pong",9);
- Thread th9= new Thread(x9);
- Ping x10= new Ping("pong",10);
- Thread th10= new Thread(x10);
- th1.start();
- th2.start();
- th3.start();
- th4.start();
- th5.start();
- th6.start();
- th7.start();
- th8.start();
- th9.start();
- th10.start();
- }
- public static void main(String[] args) {
- Hilos h = new Hilos();
- h.iniciar();
- }
- }
- class Ping implements Runnable {
- static boolean bandera = true;
- String c;
- int cual;
- public Ping(String c, int cual) {
- this.c = c;
- this.cual = cual;
- }
- public synchronized void imprimir1(){
- if(c=="ping"&&bandera==true){
- System.out.println(c+cual);
- bandera=false;
- }
- if(c.equals("pong")&&bandera==false){
- System.out.println("t"+c+cual);
- bandera=true;
- }
- }
- @Override
- public void run() {
- while(true) {
- try {
- Thread.sleep(500);
- imprimir1();
- } catch (InterruptedException ex) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement