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

Commit6b52a0f

Browse files
sample thread example
1 parent0408832 commit6b52a0f

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
packagesporadic.thread;
2+
3+
publicclassHusbandAndWifeJobimplementsRunnable {
4+
5+
/*
6+
* (non-Javadoc)
7+
*
8+
* @see java.lang.Runnable#run()
9+
*/
10+
@Override
11+
publicvoidrun() {
12+
for (inti =0;i <10;i++) {
13+
makeWithdrawl(10);
14+
if (bankAccount.getBalance() <0) {
15+
System.out.println("Overdrawn!!!");
16+
}
17+
}
18+
}
19+
20+
privateBankAccountbankAccount =newBankAccount();
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+
privatesynchronizedvoidmakeWithdrawl(intamount) {
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 (InterruptedExceptione) {
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+
publicstaticvoidmain(String[]args) {
46+
HusbandAndWifeJobhusbandAndWifeJob =newHusbandAndWifeJob();
47+
Threadhusband =newThread(husbandAndWifeJob);
48+
Threadwife =newThread(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+
classBankAccount {
59+
privateintbalance =100000;
60+
61+
publicintgetBalance() {
62+
returnbalance;
63+
}
64+
65+
publicvoidwithdraw(intwithdrawAmount) {
66+
balance =balance -withdrawAmount;
67+
}
68+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
packagesporadic.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+
publicclassThreadIsCoolimplementsRunnable{
7+
8+
publicstaticvoidmain(String []args){
9+
ThreadIsCoolthreadIsCool =newThreadIsCool();
10+
Threadabc =newThread(threadIsCool);
11+
Threaddef =newThread(threadIsCool);
12+
Threadghi =newThread(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 (InterruptedExceptione) {
23+
e.printStackTrace();
24+
}
25+
26+
def.start();
27+
try {
28+
def.join();
29+
}catch (InterruptedExceptione) {
30+
e.printStackTrace();
31+
}
32+
33+
ghi.start();
34+
try {
35+
ghi.join();
36+
}catch (InterruptedExceptione) {
37+
e.printStackTrace();
38+
}
39+
System.out.println("Now the Program ended.");
40+
}
41+
42+
@Override
43+
publicvoidrun(){
44+
for(inti =0;i <5;i++){
45+
StringthreadName =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 (InterruptedExceptione) {
52+
e.printStackTrace();
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp