/** * @author Haiping Xu * Created at the CIS Department, UMass Dartmouth */ import java.util.Random; // The class Vote is defined as a subclass of the Thread class public class Vote extends Thread { private String response; public Vote(String newResponse){ response = newResponse; } public void run(){ final int REPETITIONS = 5; Random random = new Random(); int delay = 0; for (int i = 0; i < REPETITIONS; i++){ delay = random.nextInt(1000); try { sleep(delay); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(response); } } // Thread Creation 1: One way to create a thread in Java is to define // a subclass of Thread and override the default run() method. public static void main(String[] args) { Vote yes = new Vote("Yes -- Thread 1"); Vote no = new Vote("No -- Thread 2"); yes.start(); no.start(); } }