Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ThreadsExample {
- // main method of our program
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- MyThread oddNumber = new MyThread(1);
- Thread t1 = new Thread(oddNumber);
- t1.start();
- }
- }
- class MyThread implements Runnable {
- private int timer;
- private int counter = 0;
- //constructor
- MyThread(int timer) { //modify here
- this.timer = timer;
- //code for constructor goes here
- }
- public void run() {
- //the task of the thread goes in here
- for (;;) { //infinte loop
- try {
- Thread.sleep(this.timer*1000);
- } catch (InterruptedException e) {
- return;
- }
- //do something before it sleeps/stops for the time amount of seconds
- System.out.print(counter + " ");
- counter+=2;
- }
- }
- }
- interface ChairBehaviour{
- public void spin();
- }
- class Chair implements ChairBehaviour{
- //properties / variables
- private String name;
- private String colour;
- private String material;
- private float size;
- //constructor
- public Chair(String name, String colour, String material, float size) {
- this.name = name;
- this.colour = colour;
- this.material = material;
- this.size = size;
- }
- //gets and set methods
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getColour() {
- return colour;
- }
- public void setColour(String colour) {
- this.colour = colour;
- }
- public String getMaterial() {
- return material;
- }
- public void setMaterial(String material) {
- this.material = material;
- }
- public float getSize() {
- return size;
- }
- public void setSize(float size) {
- this.size = size;
- }
- //behaviours
- public void spin() {
- System.out.println("Chair is now spinning");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment