georgeB96

Java Thread prints even numbers on timer

Oct 29th, 2021
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 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.         MyThread oddNumber = new MyThread(1);
  14.         Thread t1 = new Thread(oddNumber);
  15.         t1.start();
  16.    
  17.  
  18.     }
  19.  
  20. }
  21.  
  22.  
  23. class MyThread implements Runnable {
  24.  
  25.     private int timer;
  26.     private int counter = 0;
  27. //constructor
  28.  
  29.     MyThread(int timer) { //modify here
  30.  
  31.        
  32.         this.timer = timer;
  33. //code for constructor goes here
  34.  
  35.     }
  36.  
  37.     public void run() {
  38.        
  39. //the task of the thread goes in here
  40.  
  41.         for (;;) { //infinte loop
  42.  
  43.             try {
  44.  
  45.                 Thread.sleep(this.timer*1000);
  46.  
  47.             } catch (InterruptedException e) {
  48.  
  49.                 return;
  50.  
  51.             }
  52.             //do something before it sleeps/stops for the time amount of seconds
  53.             System.out.print(counter + " ");
  54.             counter+=2;
  55.  
  56.         }
  57.     }
  58.  
  59. }
  60.  
  61. interface ChairBehaviour{
  62.     public void spin();
  63. }
  64.  
  65. class Chair implements ChairBehaviour{
  66.  
  67. //properties / variables
  68.  
  69.     private String name;
  70.  
  71.     private String colour;
  72.  
  73.     private String material;
  74.  
  75.     private float size;
  76.  
  77. //constructor
  78.  
  79.     public Chair(String name, String colour, String material, float size) {
  80.  
  81.         this.name = name;
  82.  
  83.         this.colour = colour;
  84.  
  85.         this.material = material;
  86.  
  87.         this.size = size;
  88.  
  89.     }
  90.  
  91. //gets and set methods
  92.  
  93.     public String getName() {
  94.  
  95.         return name;
  96.  
  97.     }
  98.  
  99.     public void setName(String name) {
  100.  
  101.         this.name = name;
  102.  
  103.     }
  104.  
  105.     public String getColour() {
  106.  
  107.         return colour;
  108.  
  109.     }
  110.  
  111.     public void setColour(String colour) {
  112.  
  113.         this.colour = colour;
  114.  
  115.     }
  116.  
  117.     public String getMaterial() {
  118.  
  119.         return material;
  120.  
  121.     }
  122.  
  123.     public void setMaterial(String material) {
  124.  
  125.         this.material = material;
  126.  
  127.     }
  128.  
  129.     public float getSize() {
  130.  
  131.         return size;
  132.  
  133.     }
  134.  
  135.     public void setSize(float size) {
  136.  
  137.         this.size = size;
  138.  
  139.     }
  140.  
  141. //behaviours
  142.  
  143.     public void spin() {
  144.  
  145.         System.out.println("Chair is now spinning");
  146.  
  147.     }
  148.  
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment