Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. package me.bazhenov.bulb;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         new Thread(new Bulb("first")).start();
  7.         new Thread(new Bulb("seconds")).start();
  8.     }
  9. }
  10.  
  11. public class Bulb implements Runnable {
  12.  
  13.     private final String name;
  14.  
  15.     public Bulb(String name) {
  16.         this.name = name;
  17.     }
  18.  
  19.     public void run() {
  20.         Thread self = currentThread();
  21.         while(!self.isInterrupted()) {
  22.             System.out.println(name + " bulb is on");
  23.             try {
  24.                 sleep(300);
  25.             } catch (InterruptedException e) {
  26.                 self.interrupt();
  27.             }
  28.             System.out.println(name + " bulb is off");
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement