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

Understanding Multithreading and Synchronization in Java, Lecture notes of Java Programming

The concept of multithreading in Java, including creating and running threads, race conditions, deadlocks, and synchronization. It also provides an example of a program with multiple threads manipulating a bank account.

What you will learn

  • What is a race condition in multithreading?
  • How do you synchronize object access in Java?
  • How do you create and run a thread in Java?
  • What is a deadlock in multithreading?
  • What is multithreading in Java?

Typology: Lecture notes

2021/2022

Uploaded on 09/12/2022

unknown user
unknown user 🇺🇸

1 / 66

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Multithreading
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42

Partial preview of the text

Download Understanding Multithreading and Synchronization in Java and more Lecture notes Java Programming in PDF only on Docsity!

Multithreading

Chapter Goals

  • To understand how multiple threads can execute in parallel
  • To learn how to implement threads
  • To understand race conditions and deadlocks
  • To be able to avoid corruption of shared objects by using locks

and conditions

  • To be able to use threads for programming animations

Running a Thread

  • Implement a class that implements the Runnable interface:

public interface Runnable

void run();

  • Place the code for your task into the run method of your class:

public class MyRunnable implements Runnable

public void run()

Task statements

Running a Thread

  • Create an object of your subclass:

Runnable r = new MyRunnable();

  • Construct a Thread object from the runnable object:

Thread t = new Thread(r);

  • Call the start method to start the thread:

t.start();

GreetingRunnable Outline

public class GreetingRunnable implements Runnable

private String greeting;

public GreetingRunnable(String aGreeting)

greeting = aGreeting;

public void run()

Task statements

Thread Action for GreetingRunnable

  • Print a time stamp
  • Print the greeting
  • Wait a second

Running Threads

  • sleep puts current thread to sleep for given number of

milliseconds:

Thread.sleep(milliseconds)

  • When a thread is interrupted, most common response is to

terminate run

Generic run method

public void run()

try

Task statements

catch (InterruptedException exception)

Clean up, if necessary

ch20/greeting/GreetingRunnable.java (cont.)

22 public void run() 23 { 24 try 25 { 26 for (int i = 1; i <= REPETITIONS; i++) 27 { 28 Date now = new Date(); 29 System.out.println(now + " " + greeting); 30 Thread.sleep(DELAY); 31 } 32 } 33 catch (InterruptedException exception) 34 { 35 } 36 } 37 }

To Start the Thread

  • Construct an object of your runnable class:

Runnable t = new GreetingRunnable("Hello World");

  • Then construct a thread and call the start method:

Thread t = new Thread(r);

t.start();

ch20/greeting/GreetingThreadRunner.java (cont.)

Program Run:

Mon Dec 28 12:04:46 PST 2009 Hello, World! Mon Dec 28 12:04:46 PST 2009 Goodbye, World! Mon Dec 28 12:04:47 PST 2009 Hello, World! Mon Dec 28 12:04:47 PST 2009 Goodbye, World! Mon Dec 28 12:04:48 PST 2009 Hello, World! Mon Dec 28 12:04:48 PST 2009 Goodbye, World! Mon Dec 28 12:04:49 PST 2009 Hello, World! Mon Dec 28 12:04:49 PST 2009 Goodbye, World! Mon Dec 28 12:04:50 PST 2009 Hello, World! Mon Dec 28 12:04:50 PST 2009 Goodbye, World! Mon Dec 28 12:04:51 PST 2009 Hello, World! Mon Dec 28 12:04:51 PST 2009 Goodbye, World! Mon Dec 28 12:04:52 PST 2009 Goodbye, World! Mon Dec 28 12:04:52 PST 2009 Hello, World! Mon Dec 28 12:04:53 PST 2009 Hello, World! Mon Dec 28 12:04:53 PST 2009 Goodbye, World! Mon Dec 28 12:04:54 PST 2009 Hello, World! Mon Dec 28 12:04:54 PST 2009 Goodbye, World! Mon Dec 28 12:04:55 PST 2009 Hello, World! Mon Dec 28 12:04:55 PST 2009 Goodbye, World!

Thread Scheduler

  • Thread scheduler: runs each thread for a short amount of time

(a time slice )

  • Then the scheduler activates another thread
  • There will always be slight variations in running times -

especially when calling operating system services (e.g. input

and output)

  • There is no guarantee about the order in which threads are

executed

Self Check 20.

What would be the result of the program if the main method

called

r1.run();

r2.run();

instead of starting threads?

Answer: The first call to run would print ten “Hello”

messages, and then the second call to run would print ten

“Goodbye” messages

Terminating Threads

  • A thread terminates when its run method terminates
  • Do not terminate a thread using the deprecated stop method
  • Instead, notify a thread that it should terminate:

t.interrupt();

  • interrupt does not cause the thread to terminate – it sets a

boolean variable in the thread data structure