









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of concurrency in java, focusing on multitasking with multiple programs communicating with each other, implementing the runnable interface, and thread swapping. It also covers possible thread states and concurrency issues, with examples of thread races and synchronization. Students of cs 241 at wright state university, under the guidance of dr. T. Doom, can use this document as study notes, summaries, or cheat sheets to better understand the concepts of concurrency and threading in java.
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
-^ (2) new Thread(myRunnable); // myRunnable’s class implements Runnablepublic class MyRunnable implements Runnable {public void run() {doStuff();} // end method run… MyRunnable myRunnable = new MyRunnable();Thread t = new Thread(myRunnable); // define the method on the stackt.start();
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
public^ class
Threadrace
… public^ void^ go^
JFrame^ frame
ne^ JFrame("Thread
Racer")
JFrame^ frame
=^ new^ JFrame("Thread
Racer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize
frame.setVisible
(true); frame.setLayout
(new^ GridLayout(numberOfRacers,1)); int^ numberOfNonRacerThreads
=^ Thread.activeCount();
for^ (int^
i^ =^ 0;^ i <^ numberOfRacers;
i++)^ {
Racer^ racer
=^ new^ Racer("Racer
"^ +^ i);
frame.add(racer); Thread^ thread
=^ new^ Thread(racer
thread.start
} … } //^ end^ method
go }^ //^ end class^ ThreadRace public^ class
Main^ { public^ static
void^ main
(String[]
args)^ {
ThreadRace
theRace =^ new^ ThreadRace(3); theRace.go();} //^ end^ method
main
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
}^ // } //^ end^ class
main
import^ java.awt.;import^ javax.swing.;public^ class
Racer^ extends
JPanel implements^ Runnable
public { void^ paintComponent
(Graphics
g)^ {
g.setColor
(Color.BLACK);
set^ background
g.fillRect
(0,0,getWidth(),getHeight
g setColor
(Color^
//^ dra^
line
implements
Runnable
private^ String^ myName; private^ int^ myPosition
private^ final^ int
numberofSteps
private^ final^ int
racerHeight
private^ final^ int
racerWidth
g.setColor
(Color.BLUE);
//^ draw^ line
g.drawLine
(0,getHeight()/2,getWidth(),getHeight
g.^ setColor
(Color.RED);
//^ draw^
racer
g.fillRect
(myPosition*getWidth()/numberofSteps),
private^ final^ int
racerWidth
private^ int^ speed
public^ Racer
(String name)^ { myName^ = new^ String
(name); }^ //^ end constructor
Racer
(myPosition
getWidth()/numberofSteps), (getHeight
()^ -^ racerHeight)/2, racerWidth,
racerHeight); }^ //^ end method^ paintComponent public^ void
run^ ()^
private^ void^ pause
(int^ millisec)
//for^ (int
i^ =^ 0; //^ i
*^ millisec;
i++)^ {
//^ busy^ wait^ -^ do
nothing //}
while^ (myPosition
+^ racerWidth
<^ numberofSteps
myPosition
+=^ speed; repaint^
pause^ (5);} ThreadRace
setWinner
(myName);
//}try^ { Thread.sleep
(millisec); }^ catch^ (Exception
e)^ { e.printStackTrace
ThreadRace.setWinner
(myName); System.out.println
("Racer "^ +^ myName
"^ has^ finished
the^ race");
}^ //^ end method^ run }^ //^ end class^ Racer
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
} } //^ end^ method
pause
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
go^ ()^ { JFrame^ frame
=^ new^ JFrame("Thread
Racer");
frame.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
frame.setSize
frame.setVisible
(true); frame.setLayout
(new^ GridLayout(numberOfRacers,1)); int^ numberOfNonRacerThreads
=^ Thread.activeCount();
f^ (i t^
i^0 i <^ b^
OfR^
i++)^ {
for^ (int^
i^ =^ 0;^ i <^ numberOfRacers;
i++)^ {
Racer^ racer
=^ new^ Racer("Racer
"^ +^ i);
frame.add(racer);Thread^ thread
=^ new^ Thread(racer); thread.setPriority(thread.getPriority
()^ +^ (2i)-4);*
System.out.println(
"Racer^ " +^ i^ +^ " now^ has priority
System.out.println(
Racer^
+^ i^ +^ now^ has^ priority
thread.getPriority()
thread.start
} while^ (Thread.activeCount()
^ numberOfNonRacerThreads)
pause(10);} System.out.println("The
Race^ is over!^ " +^ winner
"^ wins!"); }^ //^ end method^ go Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
Main^ { public^ static
void^ main(String[]
args)^ {
public^ static
void^ main(String[]
args)^ {
Thread^ t; CentralBank
bank^ =^ new^ CentralBank(); System.out.printf("Creating
a^ threads
(%d^ active)\n",
Thread.activeCount()
//^ Create
Thread^ One BranchBank
branch =^ new^ BranchBank
("Branch
Alpha",bank);
t^ =^ new^ Thread^ (branch1); t.start();S^
i^ f("^ l h^ !^ (%d^
i^ )^ "^
h^ d^
i^ C^
System.out.printf("Alpha
away!^ (%d
active)\n",
Thread.activeCount());
//^ Create
Thread^ Two BranchBank
branch =^ new^ BranchBank
("Branch
Beta",^ bank);
t^ =^ new^ Thread^ (branch2); t start();t.start();System.out.printf("Beta
away!^ (%d
active)\n",
Thread.activeCount());
}^ //^ end method^ main }^ //^ end class^ Main Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
run:Creating a threads (1 active)
run:Creating a threads (1 active)
Creating a threads (1 active)Alpha away! (2 active)Beta away! (3 active)Branch Beta adds $1.00 to $0.00 New total: $1.00Branch Alpha adds $1.00 to $0.00 New total: $1.00Branch Alpha adds $1.00 to $1.00 New total: $2.00Branch Beta adds $1.00 to $1.00 New total: $2.
Creating a threads (1 active)Alpha away! (2 active)Beta away! (3 active)Branch Alpha adds $1.00 to $0.00 New total: $1.00Branch Beta adds $1.00 to $0.00 New total: $1.00Branch Alpha adds $1.00 to $1.00 New total: $2.00Branch Beta adds $1.00 to $1.00 New total: $2.
Branch Alpha adds $1.00 to $2.00 New total: $3.00Branch Beta adds $1.00 to $2.00 New total: $3.00Branch Alpha adds $1.00 to $3.00 New total: $4.00Branch Beta adds $1.00 to $3.00 New total: $4.00Branch Alpha adds $1.00 to $4.00 New total: $5.00Branch Beta adds $1.00 to $4.00 New total: $5.00Branch Alpha adds $1 00 to $5 00 New total: $6 00
Branch Alpha adds $1.00 to $2.00 New total: $3.00Branch Beta adds $1.00 to $2.00 New total: $3.00Branch Alpha adds $1.00 to $3.00 New total: $4.00Branch Beta adds $1.00 to $3.00 New total: $4.00Branch Beta adds $1.00 to $5.00 New total: $6.00Branch Beta adds $1.00 to $6.00 New total: $7.00Branch Beta adds $1 00 to $7 00 New total: $8 00
Branch Alpha adds $1.00 to $5.00 New total: $6.00Branch Beta adds $1.00 to $5.00 New total: $6.00Branch Alpha adds $1.00 to $6.00 New total: $7.00Branch Beta adds $1.00 to $6.00 New total: $7.00Branch Beta adds $1.00 to $7.00 New total: $8.00Branch Alpha adds $1.00 to $7.00 New total: $8.00Branch Alpha adds $1.00 to $8.00 New total: $9.
Branch Beta adds $1.00 to $7.00 New total: $8.00Branch Beta adds $1.00 to $8.00 New total: $9.00Branch Alpha adds $1.00 to $4.00 New total: $5.00Branch Alpha adds $1.00 to $9.00 New total: $10.00Branch Alpha adds $1.00 to $10.00 New total: $11.00Branch Beta adds $1.00 to $9.00 New total: $10.00Branch Alpha adds $1.00 to $11.00 New total: $12.
p Branch Beta adds $1.00 to $8.00 New total: $9.00Branch Alpha adds $1.00 to $9.00 New total: $10.00Branch Beta adds $1.00 to $10.00 New total: $11.00BUILD SUCCESSFUL (total time: 1 second)
p Branch Beta adds $1.00 to $12.00 New total: $13.00Branch Alpha adds $1.00 to $12.00 New total: $13.00Branch Alpha adds $1.00 to $13.00 New total: $14.
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II