Write a complete program called MyThreads which creates two

Write a complete program called MyThreads, which creates two thread classes calledPrintIntegers and PrintMessage.

Have the two thread classes perform the following tasks:

(a)    PrintIntegers displays the integers 1 to 100 on the screen.   (4mks)

(b)   PrintMessage displays a greeting message on the screen, 10 times, which includes a String username passed as an argument to the thread constructor when the thread object is created. For example if the String passed to the constructor is \"Kate\", then the thread will produce the message \"Hello Kate!\" 10 times.

Solution

Please follow the code and comments for description :

CODE :

public class MyThreads{ // thread class to run the program

public static void main(String[] args) { // main driver method
  
Runnable r = new PrintIntegers(); // class to run the thread using runnable interface
Thread t = new Thread(r); // initialisaing the first thread
t.start(); // starting the thread to run
  
Runnable r2 = new PrintMessage(\"Kate\"); // class to run the thread using runnable interface with the string passed
Thread t2 = new Thread(r2); // initialisaing the second thread
t2.start(); // starting the thread to run
}
}

class PrintIntegers implements Runnable { // class that implements the runnable interface for the thread

@Override
public void run() { // method that runs the thread
for (int i = 1; i <= 100; i++) { // loop to print the numbers till 100
System.out.println(i); // printing the thread value
}
}
}

class PrintMessage implements Runnable { // class that implements the runnable interface for the thread

public PrintMessage(String parameter) { // constructor with the string parameter passed to print the data
for(int i = 1; i <= 10; i++) { // loop to print the data for the required number of times
System.out.println(\"Hello \"+parameter+\"!\"); // printing the data
}
}
  
@Override
public void run() { // method that runs the thread
}
}


OUTPUT :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Hello Kate!
Hello Kate!
Hello Kate!
Hello Kate!
Hello Kate!
Hello Kate!
Hello Kate!
Hello Kate!
Hello Kate!
Hello Kate!


Hope this is helpful.

Write a complete program called MyThreads, which creates two thread classes calledPrintIntegers and PrintMessage. Have the two thread classes perform the follow
Write a complete program called MyThreads, which creates two thread classes calledPrintIntegers and PrintMessage. Have the two thread classes perform the follow
Write a complete program called MyThreads, which creates two thread classes calledPrintIntegers and PrintMessage. Have the two thread classes perform the follow

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site