8000 update TestThread class · fishercoder1534/RandomJava@b77e3c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b77e3c3

Browse files
update TestThread class
1 parent 36bfaf7 commit b77e3c3

File tree

1 file changed

+18
-18
lines changed
  • src/main/java/multithread/synchronization/withSynchronization

1 file changed

+18
-18
lines changed

src/main/java/multithread/synchronization/withSynchronization/TestThread.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
* When we start two or more threads within a program, there may be a situation
1313
* when multiple threads try to access the same resource and finally they can
1414
* produce unforeseen result due to concurrency issue. For example if multiple
15-
* threads try to write within a same file then they may corrupt the data
16-
* because one of the threads can overrite data or while one thread is opening
15+
* threads try to write within the same file then they may corrupt the data
16+
* because one of the threads can overwrite data or while one thread is opening
1717
* the same file at the same time another thread might be closing the same file.
1818
*
1919
* So there is a need to synchronize the action of multiple threads and make
@@ -27,11 +27,11 @@
2727
* resources within this block. Following is the general form of the
2828
* synchronized statement:
2929
*
30-
* synchronized(objectidentifier) {
30+
* synchronized(objectIdentifier) {
3131
* // Access shared variables and other shared resources
3232
* }
3333
*
34-
* Here, the objectidentifier is a reference to an object whose lock associates
34+
* Here, the objectIdentifier is a reference to an object whose lock associates
3535
* with the monitor that the synchronized statement represents. Now we are going
3636
* to see two examples where we will print a counter using two different
3737
* threads. When threads are not synchronized, they print counter value which is
@@ -40,19 +40,18 @@
4040
*/
4141
public class TestThread {
4242
public static void main(String args[]) {
43+
PrintDemo printDemo = new PrintDemo();
4344

44-
PrintDemo PD = new PrintDemo();
45+
ThreadDemo threadOne = new ThreadDemo("Thread - 1 ", printDemo);
46+
ThreadDemo threadTwo = new ThreadDemo("Thread - 2 ", printDemo);
4547

46-
ThreadDemo T1 = new ThreadDemo("Thread - 1 ", PD);
47-
ThreadDemo T2 = new ThreadDemo("Thread - 2 ", PD);
48-
49-
T1.start();
50-
T2.start();
48+
threadOne.start();
49+
threadTwo.start();
5150

5251
// wait for threads to end
5352
try {
54-
T1.join();
55-
T2.join();
53+
threadOne.join();
54+
threadTwo.join();
5655
} catch (Exception e) {
5756
System.out.println("Interrupted");
5857
}
@@ -62,29 +61,30 @@ public static void main(String args[]) {
6261
class PrintDemo {
6362
public void printCount() {
6463
try {
64+
System.out.println(Thread.currentThread().getName() + " is working now..");
6565
for (int i = 5; i > 0; i--) {
66-
System.out.println("Counter --- " + i);
66+
System.out.println("Counter --- " + i + " from thread: " + Thread.currentThread().getName());
6767
}
6868
} catch (Exception e) {
6969
System.out.println("Thread interrupted.");
7070
}
7171
}
72-
7372
}
7473

7574
class ThreadDemo extends Thread {
7675
private Thread t;
7776
private String threadName;
78-
PrintDemo PD;
77+
PrintDemo printDemo;
7978

8079
ThreadDemo(String name, PrintDemo pd) {
8180
threadName = name;
82-
PD = pd;
81+
printDemo = pd;
8382
}
8483

8584
public void run() {
86-
synchronized (PD) {//Here's all the difference between the two examples! It uses this synchronized keyword to identify the resources that need to be synchronized!
87-
PD.printCount();
85+
//Here's all the difference between the two examples! It uses this synchronized keyword to identify the resources that need to be synchronized!
86+
synchronized (printDemo) {
87+
printDemo.printCount();
8888
}
8989
System.out.println("Thread " + threadName + " exiting.");
9090
}

0 commit comments

Comments
 (0)
0