Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit45e0a8e

Browse files
synchronization demo
1 parentfd4d889 commit45e0a8e

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
*
3+
*/
4+
packagesporadic.thread.synchronization.withSynchronization;
5+
6+
/**
7+
* This produces the SAME result every time you run this program, in contrast to
8+
* the one without synchronization. Copied from this link:
9+
* http://www.tutorialspoint.com/java/java_thread_synchronization.htm, refer to
10+
* this link for further info.
11+
*
12+
* When we start two or more threads within a program, there may be a situation
13+
* when multiple threads try to access the same resource and finally they can
14+
* 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
17+
* the same file at the same time another thread might be closing the same file.
18+
*
19+
* So there is a need to synchronize the action of multiple threads and make
20+
* sure that only one thread can access the resource at a given point in time.
21+
* This is implemented using a concept called monitors. Each object in Java is
22+
* associated with a monitor, which a thread can lock or unlock. Only one thread
23+
* at a time may hold a lock on a monitor.
24+
*
25+
* Java programming language provides a very handy way of creating threads and
26+
* synchronizing their task by using synchronized blocks. You keep shared
27+
* resources within this block. Following is the general form of the
28+
* synchronized statement:
29+
*
30+
* synchronized(objectidentifier) {
31+
* // Access shared variables and other shared resources
32+
* }
33+
*
34+
* Here, the objectidentifier is a reference to an object whose lock associates
35+
* with the monitor that the synchronized statement represents. Now we are going
36+
* to see two examples where we will print a counter using two different
37+
* threads. When threads are not synchronized, they print counter value which is
38+
* not in sequence, but when we print counter by putting inside synchronized()
39+
* block, then it prints counter very much in sequence for both the threads.
40+
*/
41+
publicclassTestThread {
42+
publicstaticvoidmain(Stringargs[]) {
43+
44+
PrintDemoPD =newPrintDemo();
45+
46+
ThreadDemoT1 =newThreadDemo("Thread - 1 ",PD);
47+
ThreadDemoT2 =newThreadDemo("Thread - 2 ",PD);
48+
49+
T1.start();
50+
T2.start();
51+
52+
// wait for threads to end
53+
try {
54+
T1.join();
55+
T2.join();
56+
}catch (Exceptione) {
57+
System.out.println("Interrupted");
58+
}
59+
}
60+
}
61+
62+
classPrintDemo {
63+
publicvoidprintCount() {
64+
try {
65+
for (inti =5;i >0;i--) {
66+
System.out.println("Counter --- " +i);
67+
}
68+
}catch (Exceptione) {
69+
System.out.println("Thread interrupted.");
70+
}
71+
}
72+
73+
}
74+
75+
classThreadDemoextendsThread {
76+
privateThreadt;
77+
privateStringthreadName;
78+
PrintDemoPD;
79+
80+
ThreadDemo(Stringname,PrintDemopd) {
81+
threadName =name;
82+
PD =pd;
83+
}
84+
85+
publicvoidrun() {
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();
88+
}
89+
System.out.println("Thread " +threadName +" exiting.");
90+
}
91+
92+
publicvoidstart() {
93+
System.out.println("Starting " +threadName);
94+
if (t ==null) {
95+
t =newThread(this,threadName);
96+
t.start();
97+
}
98+
}
99+
100+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
*
3+
*/
4+
packagesporadic.thread.synchronization.withoutSynchronization;
5+
6+
/**
7+
* @author jiahuan
8+
* This produces DIFFERENT result every time you run this program, in contrast to the one with synchronization.
9+
* Here is a simple example which may or may not print counter value in sequence and every time we run it, it produces different result based on CPU availability to a thread.
10+
* Copied from this link: http://www.tutorialspoint.com/java/java_thread_synchronization.htm, refer to this link for further info.
11+
*
12+
*/
13+
publicclassTestThread {
14+
publicstaticvoidmain(Stringargs[]) {
15+
16+
PrintDemoPD =newPrintDemo();
17+
18+
ThreadDemoT1 =newThreadDemo("Thread - 1 ",PD );
19+
ThreadDemoT2 =newThreadDemo("Thread - 2 ",PD );
20+
21+
T1.start();
22+
T2.start();
23+
24+
// wait for threads to end
25+
try {
26+
T1.join();
27+
T2.join();
28+
}catch(Exceptione) {
29+
System.out.println("Interrupted");
30+
}
31+
}
32+
33+
}
34+
35+
36+
classPrintDemo {
37+
publicvoidprintCount(){
38+
try {
39+
for(inti =5;i >0;i--) {
40+
System.out.println("Counter --- " +i );
41+
}
42+
}catch (Exceptione) {
43+
System.out.println("Thread interrupted.");
44+
}
45+
}
46+
47+
}
48+
49+
classThreadDemoextendsThread {
50+
privateThreadt;
51+
privateStringthreadName;
52+
PrintDemoPD;
53+
54+
ThreadDemo(Stringname,PrintDemopd){
55+
threadName =name;
56+
PD =pd;
57+
}
58+
publicvoidrun() {
59+
PD.printCount();
60+
System.out.println("Thread " +threadName +" exiting.");
61+
}
62+
63+
publicvoidstart ()
64+
{
65+
System.out.println("Starting " +threadName );
66+
if (t ==null)
67+
{
68+
t =newThread (this,threadName);
69+
t.start ();
70+
}
71+
}
72+
73+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp