Skip to content

Commit 78c1d8e

Browse files
Initial Commit
0 parents  commit 78c1d8e

5 files changed

+176
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
out
2+
.idea/
3+
*.iml

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# How to use Synchronized keyword in Java
2+
3+
This code is part of a blog post.
4+
5+
The link to the blog post is given here.
6+
7+
Read the blog post to make the best use of this repo :)
8+
9+
10+
## Cloning the code to your local
11+
12+
Clone this code to your local using the following command
13+
14+
```bash
15+
git clone https://github.com/aditya-sridhar/java-threads-synchronized-example.git
16+
```
17+
18+
Or you can import the project into an IDE of your choice. I Used Eclipse to test this out
19+
20+
## Run the Application
21+
22+
Each Code in this Repo has a **main()** function. So each code can be run independently.

src/SynchronizedCustomObjectDemo.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Utility to print from 0 to 5
3+
*/
4+
class PrintUtilExample3 {
5+
6+
//object to store the synchronized lock
7+
final Object lockObject = new Object();
8+
9+
/**
10+
* Function to print numbers from 0 to 5
11+
*/
12+
public void printNumbers() {
13+
synchronized(lockObject) {
14+
for (int i = 0; i <= 5; i++) {
15+
System.out.println(Thread.currentThread().getName() + ": " + i);
16+
try {
17+
Thread.sleep(100);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
}
23+
}
24+
}
25+
26+
/**
27+
* Thread worker
28+
*/
29+
class RunnableWorkerExample3 implements Runnable {
30+
PrintUtilExample3 pu3;
31+
public RunnableWorkerExample3(PrintUtilExample3 pu3){
32+
this.pu3 = pu3;
33+
}
34+
@Override
35+
public void run() {
36+
pu3.printNumbers();
37+
}
38+
}
39+
40+
/**
41+
* Main Class
42+
*/
43+
public class SynchronizedCustomObjectDemo {
44+
public static void main(String[] args) {
45+
PrintUtilExample3 pu3 = new PrintUtilExample3();
46+
Runnable r = new RunnableWorkerExample3(pu3);
47+
Thread t1 = new Thread(r);
48+
Thread t2 = new Thread(r);
49+
Thread t3 = new Thread(r);
50+
51+
t1.start();
52+
t2.start();
53+
t3.start();
54+
}
55+
}

src/SynchronizedMethodDemo.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Utility to print numbers from 0 to 5
3+
*/
4+
class PrintUtil {
5+
/**
6+
* Function to print numbers from 0 to 5
7+
*/
8+
public synchronized void printNumbers() {
9+
for (int i = 0; i <= 5; i++) {
10+
System.out.println(Thread.currentThread().getName() + ": " + i);
11+
try {
12+
//simulates delay
13+
Thread.sleep(100);
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
}
18+
}
19+
}
20+
21+
/**
22+
* Thread worker
23+
*/
24+
class RunnableWorker implements Runnable {
25+
PrintUtil pu;
26+
public RunnableWorker(PrintUtil pu) {
27+
this.pu = pu;
28+
}
29+
30+
@Override
31+
public void run() {
32+
pu.printNumbers();
33+
}
34+
}
35+
36+
/**
37+
* Main Class
38+
*/
39+
public class SynchronizedMethodDemo {
40+
public static void main(String[] args) {
41+
PrintUtil pu = new PrintUtil();
42+
Runnable r = new RunnableWorker(pu);
43+
Thread t1 = new Thread(r);
44+
Thread t2 = new Thread(r);
45+
Thread t3 = new Thread(r);
46+
47+
t1.start();
48+
t2.start();
49+
t3.start();
50+
}
51+
}

src/SynchronizedStaticMethodDemo.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Utility to print numbers from 0 to 5
3+
*/
4+
class PrintUtilExample2 {
5+
/**
6+
* Function to print numbers form 0 to 5
7+
*/
8+
public static synchronized void printNumbers() {
9+
for (int i = 0; i <= 5; i++) {
10+
System.out.println(Thread.currentThread().getName() + ": " + i);
11+
try {
12+
//Sleep is used to simulate delay in thread operations
13+
Thread.sleep(100);
14+
} catch (InterruptedException e) {
15+
e.printStackTrace();
16+
}
17+
}
18+
}
19+
}
20+
21+
/**
22+
* Thread worker
23+
*/
24+
class RunnableWorkerExample2 implements Runnable {
25+
@Override
26+
public void run() {
27+
PrintUtilExample2.printNumbers();
28+
}
29+
}
30+
31+
/**
32+
* Main Class
33+
*/
34+
public class SynchronizedStaticMethodDemo {
35+
public static void main(String[] args) {
36+
Runnable r = new RunnableWorkerExample2();
37+
Thread t1 = new Thread(r);
38+
Thread t2 = new Thread(r);
39+
Thread t3 = new Thread(r);
40+
41+
t1.start();
42+
t2.start();
43+
t3.start();
44+
}
45+
}

0 commit comments

Comments
 (0)