georgeB96

Class rev

Dec 3rd, 2021
738
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Random;
  2.  
  3. public class classTest {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         ClassThatMakesThreads t1 = new ClassThatMakesThreads(5000);
  8.         Thread th1 = new Thread(t1);
  9.         th1.start();
  10.        
  11.        
  12.         ClassThatMakesThreads t2= new ClassThatMakesThreads(3000);
  13.         Thread th2 = new Thread(t2);
  14.         th2.start();
  15.        
  16.        
  17.         System.out.println("Hello world");
  18.        
  19.     }
  20.  
  21. }
  22.  
  23. class ClassThatMakesThreads implements Runnable {
  24.    
  25.     private int timer;
  26.    
  27.     public ClassThatMakesThreads(int timer) {
  28.         this.timer = timer;
  29.     }
  30.    
  31.    
  32.     public void run() {
  33.        
  34.         for(;;) {
  35.             System.out.println((int)Math.floor(Math.random()*90000+10000));
  36.        
  37.            
  38.                 try {
  39.                     Thread.sleep(timer);
  40.                    
  41.                 } catch (Exception e) {
  42.                     // in a thread sleep situation,
  43.                     //it is very unlikely that the program will reach this block of code
  44.                     System.out.println("Wrong sleep");
  45.                 }
  46.            
  47.  
  48.         }
  49.     }
  50. }
  51.  
  52.    
  53.    
  54. //Create a thread that prints in the console every 5 seconds a random amount of 5 digits long
  55.  
  56.  
  57.  
  58.  
  59. //
  60. ////Create an interface called DogRules.
  61. ////This interface should contain a public method called  bark() and should return void
  62. //
  63. //interface DogRules{
  64. //  public void bark();
  65. //}
  66. //
  67. //class Dog implements DogRules{
  68. //
  69. //  private String name, breed, age;
  70. //
  71. //  public Dog(String name, String breed, String age) {
  72. //         
  73. //      this.name = name;
  74. //      this.breed = breed;
  75. //      this.age = age;
  76. //  }
  77. //
  78. //  public  void bark() {
  79. //  System.out.println(name + ": woof");
  80. //  }
  81. //}
  82.  
  83.  
  84.  
  85. // Define a class with the following props: A dog class with name, breed and age
  86. // The class should have a constructor setting up all the properties of the class
  87.  
  88. //class Dog {
  89. // 
  90. //  private String name, breed, age;
  91. // 
  92. //  public Dog(String name, String breed, String age) {
  93. //     
  94. //      this.name = name;
  95. //      this.breed = breed;
  96. //      this.age = age;
  97. //  }
  98. //}
  99.  
  100. //The class should have a constructor setting up no properties
  101. //
  102. //class Dog{
  103. // 
  104. //  private String name;
  105. //  private String age;
  106. //  private String breed;
  107. // 
  108. // 
  109. //  public Dog() {
  110. //     
  111. //  }
  112. // 
  113. //}
  114.  
  115. //This class should have a constructor that takes no variables but sets all available properties to empty strings
  116.  
  117. //
  118. //class Dog{
  119. //
  120. //
  121. //  private String name;
  122. //  private String age;
  123. //  private String breed;
  124. //
  125. //  public Dog() {
  126. //      this.name = "Erin";
  127. //      this.age = "";
  128. //      this.breed = "";
  129. //  }
  130. // 
  131. //  public void bark() {
  132. //     
  133. //      System.out.println(name + ": woof");
  134. //     
  135. //  }
  136. // 
  137. //}
  138.  
  139.  
  140.  
  141. ////Dog dog = new Dog("Erin", "Pug", "10");
  142. ////dog.bark();
  143. //
  144. ////create the object (t1) using the runnable class
  145. ////or the class that is using the runnable interface
  146. //ThreadClass t1 = new ThreadClass();
  147. //
  148. ////create a thread object (th) and pass the runnable object(t1) in the constructor
  149. //// of the thread (Thread(t1))
  150. //Thread th = new Thread(t1);
  151. //
  152. ////start the thread or
  153. //// execute the block of code inside the run function.
  154. //th.start();
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
RAW Paste Data