Advertisement
hawzze

PingPong-threads with implementation

May 17th, 2021
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. package uni.fmi.bachelors;
  2.  
  3. public class RunPingPong implements Runnable{
  4.     String word;
  5.     int time;
  6.  
  7.     public RunPingPong(String word, int time) {
  8.             this.word=word;
  9.             this.time=time;
  10.     }
  11.  
  12.     @Override
  13.     public void run() {
  14.         for(int i=1; i<=5; i++){
  15.             System.out.println(word);
  16.             try{
  17.                 Thread.sleep(time);
  18.             }catch (InterruptedException e){
  19.                 //to do auto genereted stub
  20.                 e.printStackTrace();
  21.             }
  22.         }
  23.  
  24.     }
  25.  
  26.     public static void main(String[] args) {
  27.             RunPingPong ping=new RunPingPong("Ping",100);
  28.         RunPingPong pong=new RunPingPong("\tPong",100);
  29.         Thread t1=new Thread(ping);
  30.         Thread t2=new Thread(pong);
  31.         t1.start();
  32.         t2.start();
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement