You are on page 1of 5

/home/mddilshad/Desktop/assignment/Uppercase/src/uppercase/Uppercase.

java
Page 1 of 3 Thu 04 Jun 2020 09:34:47 PM +06
1 /*
2 * To change this license header, choose License Headers in Project
3 Properties.
4 * To change this template file, choose Tools | Templates
5 * and open the template in the editor.
6 */
7
8 //package uppercase;
9
10 import java.util.Scanner;
11 import java.util.logging.Level;
12 import java.util.logging.Logger;
13
14 /**
15 * @author Abdullah Al Mamun
16 */
17 class Server {
18 private String sentence; //property
19 void setSentence(String x){
20 sentence = x;
21 }
22 String getSentence(){
23 return sentence;
24 }
25 void process(String threadName)
26 {
27 // process your string given by user
28 System.out.println();
29 System.out.println("You are inside the Server "+threadName);
30 System.out.println("Enter your sentence: ");
31 Scanner sc = new Scanner(System.in);
32 sentence = sc.nextLine();
33 }
34 void changetoUpperCase(String msg)
35 {
36 // change the case of your string to uppercase
37 sentence = msg.toUpperCase();
38 }
39 void changetoLowerCase(String msg)
40 {
41 // change the case of your string to LowerCase
42 sentence = msg.toLowerCase();
43 }
44 void printMessage(String msg)
45 {
46 // Print the string sent in this method as parameter
47 System.out.println("[ "+msg+" ]");
48 // send the thread in sleep mode for 1000 ms after printing "
49 try{
50 Thread.sleep(1000);
51 }catch(InterruptedException ex){
52 ex.printStackTrace();
53 }

- 1 -
/home/mddilshad/Desktop/assignment/Uppercase/src/uppercase/Uppercase.java
Page 2 of 3 Thu 04 Jun 2020 09:34:47 PM +06
54 }
55 }
56
57 class ChangeCase implements Runnable {
58 String threadName;// store the Thread name
59 Server target = new Server();// initialize the server class
60 Thread t;// holds the Thread itself
61 public void changeCase(Server c, String s)
62 {
63
64 System.out.println("Press 1 to convert Uppercase or Press 2 to convert
Lowercase");
65 int i;
66 i = new Scanner(System.in).nextInt();
67 if(i == 1){
68 c.changetoUpperCase(s);
69 c.printMessage(c.getSentence());
70 return;
71 }
72 if(i == 2){
73 c.changetoLowerCase(s);
74 c.printMessage(c.getSentence());
75 return;
76 }
77 System.out.println("Invalid number given");
78 }
79 @Override
80 public synchronized void run() {
81 while(true)
82 {
83 // Use the instance of Server Class "target" to process the
84 // input string given by user
85 t = Thread.currentThread();
86 target.process(t.getName());
87 changeCase(target,target.getSentence());
88 try{
89 wait(100);
90 }catch(InterruptedException ex){
91 ex.printStackTrace();
92 }
93 notify();
94 }
95 }
96 }
97
98 public class Uppercase {
99 public static void main(String[] args){
100 ChangeCase caseChange = new ChangeCase();
101 Thread thread1 = new Thread(caseChange);
102 Thread thread2 = new Thread(caseChange);
103 Thread thread3 = new Thread(caseChange);
104 thread1.setName("Thread1");
105 thread2.setName("Thread2");

- 2 -
/home/mddilshad/Desktop/assignment/Uppercase/src/uppercase/Uppercase.java
Page 3 of 3 Thu 04 Jun 2020 09:34:47 PM +06
106 thread3.setName("Thread3");
107 thread1.start();
108 thread2.start();
109 thread3.start();
110 }
111 }
112

- 3 -
Output:
A short note on exception:
Exceptions separate error handling code from regular code. Exceptions propagate errors up the call
stack. Exception classes group and differentiate error types. One can group errors by their generalize
parent class, or differentiate errors by their actual class. Exceptions standardize error handling.

Try{
Thread.sleep(2000);

}catch(InterruptedException ex){

ex.printStackTrace();

A short note on Sleep():


sleep() method causes the currently executing thread to sleep (temporarily cease execution) for the
specified number of milliseconds.

A short note on Stop():


Whenever we want to stop a thread from running further, we may do so by calling its stop( )
method. This method causes the thread to move to the dead state.

A short note on Suspend():


It suspends the thread on which it is invoked. If the target thread holds a lock on the monitor
protecting a critical system resource when it is suspended, no thread can access this resource until
the target thread is resumed.

A short note on Resume():


Its used with suspend( ) and very much dead lock prone. It's deprecated and can not be used
without suspend() which is also deprecated.

You might also like