Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Multithreading - Java Programming Language - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Java Programming Language which includes Applet Class, Passing Parameters to Applets, Conversions, Applications and Applets, Running a Program, Applet, Application, Mouse Event, Keyboard Event etc. Key important points are: Multithreading, Threads Concept, Creating Threads, Extending, Implementing, Runnable Interface, Controlling Threads, Thread Status, Thread Groups, Synchronization

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

200 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 13: Multithreading
Threads Concept
Creating Threads by Extending the Thread
class
Creating Threads by Implementing the
Runnable Interface
Controlling Threads and Thread Status
Thread Groups
Synchronization
Creating Threads for Applets
Case Studies
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Multithreading - Java Programming Language - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Chapter 13: Multithreading

  • Threads Concept
  • Creating Threads by Extending the Thread class
  • Creating Threads by Implementing the Runnable Interface
  • Controlling Threads and Thread Status
  • Thread Groups
  • Synchronization
  • Creating Threads for Applets
  • Case Studies

Threads Concept

Multiple threads on multiple CPUs

Multiple threads sharing a single CPU

Thread 3

Thread 2

Thread 1

Thread 3

Thread 2

Thread 1

Example 13.

Using the Thread Class to Create

and Launch Threads

  • Objective: Create and run three threads:
    • The first thread prints the letter a 100 times.
    • The second thread prints the letter b 100 times.
    • The third thread prints the integers 1 through 100.

Example 13.

Using the Thread Class to

Create and Launch Threads,

cont.

TestThread

Run Click the Run button to access the DOS prompt; then type java TestThread

Example 13.

Using the Runnabel Interface

to Create and Launch Threads

  • Objective: Create and run three threads:
    • The first thread prints the letter a 100

times.

  • The second thread prints the letter b 100

times.

  • The third thread prints the integers 1

through 100.

TestRunnable

Run Click the Run button to access the DOS prompt;then type java TestRunnable Docsity.com

Controlling Threads

and Thread States

  • void run() Invoked by the Java runtime system to execute the thread. You must override this method and provide the code you want your thread to execute.
  • void start() Starts the thread, which causes the run() method to be invoked. Called by the runnable object in the client class.
  • static void sleep(long millis) throws InterruptedException Puts the runnable object to sleep for a specified time in milliseconds. Docsity.com

Thread Priority

  • Each thread is assigned a default priority of Thread.NORM_PRIORITY. You can reset the priority using setPriority(int priority).
  • Some constants for priorities include Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY

Thread States

Thread created

new

ready

running

finished

blocked

start

run

yield, or time expired

stop or complete stop

suspend, sleep, or wait

stop

resume, notify, or notifyAll

Thread Groups, cont.

  • To find out how many threads in a group are currently running, use the activeCount() method: System.out.println("The number of “
    • “ runnable threads in the group ” + g.activeCount());

Synchronization

A shared resource may be corrupted if it is accessed simultaneously by multiple threads. For example, two unsynchronized threads accessing the same bank account causes conflict.

Step balance thread[i] thread[j] 1 0 newBalance =^ bank.getBalance() + 1; 2 0 newBalance =^ bank.getBalance() + 1; 3 1 bank.setBalance(newBalance); 4 1 bank.setBalance(newBalance);

Example 13.3, cont

PiggyBankWithoutSync Run

PiggyBank -balance +getBalance+setBalance

1

100

PiggyBankWithoutSync -PiggyBank bank-Thread[] thread +main

Object

Object

AddAPennyThread

+run()

Thread

1

1

The synchronized keyword

To avoid resource conflicts, Java uses the keyword synchronized to synchronize method invocation so that only one thread can be in a method at a time. To correct the data-corruption problem in Example 13.3, you can rewrite the program as follows:

PiggyBankWithSync Run

Creating Threads for Applets

public void start() { while (true) { stillClock.repaint(); try { Thread.sleep(1000); } catch(InterruptedException ex) { } } }

What is wrong in this code? As long as the while loop is running, the browser cannot serve any other event that might be occurring.

Creating a Thread to run the

while loop

public class MyApplet extends JApplet implements Runnable { private Thread timer = null; public void init() { timer = new Thread(this); timer.start(); } ... public void run() { ... } }