Skip to content

Commit 6b52a0f

Browse files
sample thread example
1 parent 0408832 commit 6b52a0f

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package sporadic.thread;
2+
3+
public class HusbandAndWifeJob implements Runnable {
4+
5+
/*
6+
* (non-Javadoc)
7+
*
8+
* @see java.lang.Runnable#run()
9+
*/
10+
@Override
11+
public void run() {
12+
for (int i = 0; i < 10; i++) {
13+
makeWithdrawl(10);
14+
if (bankAccount.getBalance() < 0) {
15+
System.out.println("Overdrawn!!!");
16+
}
17+
}
18+
}
19+
20+
private BankAccount bankAccount = new BankAccount();
21+
22+
/**synchronized is the key here to let one thread finish, and then let the other thread to kick in.
23+
* Without synchronized this keyword, two threads will invoke this method randomly based on how the OS
24+
* scheduler schedules it.*/
25+
private synchronized void makeWithdrawl(int amount) {
26+
if (bankAccount.getBalance() >= amount) {
27+
System.out.println(Thread.currentThread().getName() + " is about to withdraw: " + amount);
28+
try {
29+
System.out.println(Thread.currentThread().getName() + " is going to sleep.");
30+
Thread.sleep(1000);
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
}
34+
System.out.println(Thread.currentThread().getName() + " woke up.");
35+
bankAccount.withdraw(amount);
36+
System.out.println(Thread.currentThread().getName() + " finished withdrawl: " + amount + "\t now the balance is: " + bankAccount.getBalance());
37+
} else {
38+
System.out.println("Sorry, not enough balance for " + Thread.currentThread().getName());
39+
}
40+
}
41+
42+
/**
43+
* @param args
44+
*/
45+
public static void main(String[] args) {
46+
HusbandAndWifeJob husbandAndWifeJob = new HusbandAndWifeJob();
47+
Thread husband = new Thread(husbandAndWifeJob);
48+
Thread wife = new Thread(husbandAndWifeJob);
49+
husband.setName("Steve Sun");
50+
wife.setName("Sophie Yan");
51+
wife.start();
52+
husband.start();
53+
System.out.println("Program ended.\n\n\n\n\n\n");
54+
}
55+
56+
}
57+
58+
class BankAccount {
59+
private int balance = 100000;
60+
61+
public int getBalance() {
62+
return balance;
63+
}
64+
65+
public void withdraw(int withdrawAmount) {
66+
balance = balance - withdrawAmount;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package sporadic.thread;
2+
3+
/** This is a cool and small program to show that threads don't run in the order that you can control, it's all scheduled by the thing called
4+
* Thread Scheduler.*/
5+
6+
public class ThreadIsCool implements Runnable{
7+
8+
public static void main(String [] args){
9+
ThreadIsCool threadIsCool = new ThreadIsCool();
10+
Thread abc = new Thread(threadIsCool);
11+
Thread def = new Thread(threadIsCool);
12+
Thread ghi = new Thread(threadIsCool);
13+
abc.setName("abc");
14+
def.setName("def");
15+
ghi.setName("ghi");
16+
System.out.println("Now the three threads kick off:");
17+
18+
abc.start();
19+
try {
20+
/* Start second thread(def) only when first thread(abc) is dead*/
21+
abc.join();
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
}
25+
26+
def.start();
27+
try {
28+
def.join();
29+
} catch (InterruptedException e) {
30+
e.printStackTrace();
31+
}
32+
33+
ghi.start();
34+
try {
35+
ghi.join();
36+
} catch (InterruptedException e) {
37+
e.printStackTrace();
38+
}
39+
System.out.println("Now the Program ended.");
40+
}
41+
42+
@Override
43+
public void run(){
44+
for(int i = 0; i < 5; i++){
45+
String threadName = Thread.currentThread().getName();
46+
System.out.println(threadName + " is running!");
47+
}
48+
System.out.println(Thread.currentThread().getName() + " is sleeping for 3 seconds");
49+
try {
50+
Thread.sleep(3000);
51+
} catch (InterruptedException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)