Skip to Content
Java Coding Problems
book

Java Coding Problems

by Anghel Leonard
September 2019
Intermediate to advanced
816 pages
18h 47m
English
Packt Publishing
Content preview from Java Coding Problems

The BLOCKED state

When a thread is trying to execute I/O tasks or synchronized blocks, it may enter into the BLOCKED state. For example, if a thread, t1, tries to enter into a synchronized block of code that is already being accessed by another thread, t2, then t1 is kept in the BLOCKED state until it can acquire the lock.

This scenario is shaped in the following snippet of code:

  1. Create two threads: t1 and t2.
  2. Start t1 via the start() method:
    1. t1 will execute the run() method and will acquire the lock for the synchronized method, syncMethod().
    2. The syncMethod() will keep t1 inside forever, since it has an infinite loop.
  3. After two seconds (arbitrary time), start t2 via the start() method:
    1. t2 will execute the run() code and will end up in ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Coding Problems - Second Edition

Java Coding Problems - Second Edition

Anghel Leonard
Java in a Nutshell, 7th Edition

Java in a Nutshell, 7th Edition

Benjamin J. Evans, David Flanagan
The Well-Grounded Java Developer, Second Edition

The Well-Grounded Java Developer, Second Edition

Benjamin Evans, Martijn Verburg, Jason Clark

Publisher Resources

ISBN: 9781789801415Supplemental Content