georgeB96

Untitled

Oct 29th, 2021
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class ThreadsExample {
  4.  
  5. // main method of our program
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.  
  12.  
  13.         new Thread(new MyThread("I am thread one!", 5)).start();
  14.         new Thread(new MyThread("I am thread two!", 10)).start();
  15.         new Thread(new MyThread("I am thread three!", 1)).start();
  16.    
  17.  
  18.        
  19. // new Thread(sleepingThread).start();
  20.  
  21. // Chair c1 = new Chair("Chair1", "blue", "plastic", 22.52f);
  22. //      c1.setName("whatevelse!;")
  23.  
  24. // Chair c2 = new Chair("Chair2", "red", "plastic", 15.2f);
  25.  
  26. // c2.spin();
  27.  
  28. //
  29.  
  30. //// String name = sc.nextLine();
  31.  
  32. //
  33.  
  34. //// Chair c3 = new Chair(name, colour, material, size);
  35.  
  36. //
  37.  
  38. // System.out.println("Chair 2 name is:" + c2.getName());
  39.  
  40.     }
  41.  
  42. }
  43.  
  44.  
  45. class MyThread implements Runnable {
  46.  
  47.     private String message;
  48.     private int timer;
  49.  
  50. //constructor
  51.  
  52.     MyThread(String message, int timer) {
  53.  
  54.         this.message = message;
  55.         this.timer = timer;
  56. //code for constructor goes here
  57.  
  58.     }
  59.  
  60.     public void run() {
  61.  
  62. //the task of the thread goes in here
  63.  
  64.         for (;;) { //infinte loop
  65.  
  66.             try {
  67.  
  68.                 Thread.sleep(this.timer*1000);
  69.  
  70.             } catch (InterruptedException e) {
  71.  
  72.                 return;
  73.  
  74.             }
  75.  
  76.             System.out.println(this.message);
  77.  
  78.         }
  79.     }
  80.  
  81. }
  82.  
  83. interface ChairBehaviour{
  84.     public void spin();
  85. }
  86.  
  87. class Chair implements ChairBehaviour{
  88.  
  89. //properties / variables
  90.  
  91.     private String name;
  92.  
  93.     private String colour;
  94.  
  95.     private String material;
  96.  
  97.     private float size;
  98.  
  99. //constructor
  100.  
  101.     public Chair(String name, String colour, String material, float size) {
  102.  
  103.         this.name = name;
  104.  
  105.         this.colour = colour;
  106.  
  107.         this.material = material;
  108.  
  109.         this.size = size;
  110.  
  111.     }
  112.  
  113. //gets and set methods
  114.  
  115.     public String getName() {
  116.  
  117.         return name;
  118.  
  119.     }
  120.  
  121.     public void setName(String name) {
  122.  
  123.         this.name = name;
  124.  
  125.     }
  126.  
  127.     public String getColour() {
  128.  
  129.         return colour;
  130.  
  131.     }
  132.  
  133.     public void setColour(String colour) {
  134.  
  135.         this.colour = colour;
  136.  
  137.     }
  138.  
  139.     public String getMaterial() {
  140.  
  141.         return material;
  142.  
  143.     }
  144.  
  145.     public void setMaterial(String material) {
  146.  
  147.         this.material = material;
  148.  
  149.     }
  150.  
  151.     public float getSize() {
  152.  
  153.         return size;
  154.  
  155.     }
  156.  
  157.     public void setSize(float size) {
  158.  
  159.         this.size = size;
  160.  
  161.     }
  162.  
  163. //behaviours
  164.  
  165.     public void spin() {
  166.  
  167.         System.out.println("Chair is now spinning");
  168.  
  169.     }
  170.  
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment