• Complain

Friesen - Java Threads and the Concurrency Utilities

Here you can read online Friesen - Java Threads and the Concurrency Utilities full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. City: Berkeley;CA;New York, year: 2015, publisher: Apress, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Friesen Java Threads and the Concurrency Utilities
  • Book:
    Java Threads and the Concurrency Utilities
  • Author:
  • Publisher:
    Apress
  • Genre:
  • Year:
    2015
  • City:
    Berkeley;CA;New York
  • Rating:
    3 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 60
    • 1
    • 2
    • 3
    • 4
    • 5

Java Threads and the Concurrency Utilities: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Java Threads and the Concurrency Utilities" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Friesen: author's other books


Who wrote Java Threads and the Concurrency Utilities? Find out the surname, the name of the author of the book and a list of all author's works by series.

Java Threads and the Concurrency Utilities — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Java Threads and the Concurrency Utilities" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Part I
Thread APIs
Jeff Friesen 2015
Jeff Friesen Java Threads and the Concurrency Utilities 10.1007/978-1-4842-1700-9_1
1. Threads and Runnables
Jeff Friesen 1
(1)
Manitoba, Canada
Electronic supplementary material
The online version of this chapter (doi: 10.1007/978-1-4842-1700-9_1 ) contains supplementary material, which is available to authorized users.
Java applications execute via threads , which are independent paths of execution through an applications code. When multiple threads are executing, each threads path can differ from other thread paths. For example, a thread might execute one of a switch statements case s, and another thread might execute another of this statements case s.
Each Java application has a default main thread that executes the main() method. The application can also create threads to perform time-intensive tasks in the background so that it remains responsive to its users. These threads execute code sequences encapsulated in objects that are known as runnables .
The Java virtual machine (JVM) gives each thread its own JVM stack to prevent threads from interfering with each other. Separate stacks let threads keep track of their next instructions to execute, which can differ from thread to thread. The stack also provides a thread with its own copy of method parameters, local variables, and return value.
Java supports threads primarily through its java.lang.Thread class and java.lang.Runnable interface. This chapter introduces you to these types.
Introducing Thread and Runnable
The Thread class provides a consistent interface to the underlying operating systems threading architecture. (The operating system is typically responsible for creating and managing threads.) A single operating system thread is associated with a Thread object.
The Runnable interface supplies the code to be executed by the thread thats associated with a Thread object. This code is located in Runnable s void run() methoda thread receives no arguments and returns no value, although it might throw an exception, which I discuss in .
Creating Thread and Runnable Objects
Except for the default main thread, threads are introduced to applications by creating the appropriate Thread and Runnable objects. Thread declares several constructors for initializing Thread objects. Several of these constructors require a Runnable object as an argument.
There are two ways to create a Runnable object. The first way is to create an anonymous class that implements Runnable , as follows:
Runnable r = new Runnable()
{
@Override
public void run()
{
// perform some work
System.out.println("Hello from thread");
}
};
Before Java 8, this was the only way to create a runnable. Java 8 introduced the lambda expression to more conveniently create a runnable:
Runnable r = () -> System.out.println("Hello from thread");
The lambda is definitely less verbose than the anonymous class. Ill use both language features throughout this and subsequent chapters.
Note
A lambda expression ( lambda ) is an anonymous function thats passed to a constructor or method for subsequent execution. Lambdas work with functional interfaces (interfaces that declare single abstract methods), such as Runnable .
After creating the Runnable object, you can pass it to a Thread constructor that receives a Runnable argument. For example, Thread(Runnable runnable) initializes a new Thread object to the specified runnable . The following code fragment demonstrates this task:
Thread t = new Thread(r);
A few constructors dont take Runnable arguments. For example, Thread() doesnt initialize Thread to a Runnable argument. You must extend Thread and override its run() method ( Thread implements Runnable ) to supply the code to run, which the following code fragment accomplishes:
class MyThread extends Thread
{
@Override
public void run()
{
// perform some work
System.out.println("Hello from thread");
}
}
// ...
MyThread mt = new MyThread();
Getting and Setting Thread State
A Thread object associates state with a thread. This state consists of a name, an indication of whether the thread is alive or dead, the execution state of the thread (is it runnable?), the threads priority, and an indication of whether the thread is daemon or nondaemon.
Getting and Setting a Threads Name
A Thread object is assigned a name, which is useful for debugging. Unless a name is explicitly specified, a default name that starts with the Thread- prefix is chosen. You can get this name by calling Thread s String getName() method. To set the name, pass it to a suitable constructor, such as Thread ( Runnable r, String name) , or call Thread s void setName( String name) method. Consider the following code fragment:
Thread t1 = new Thread(r, "thread t1");
System.out.println(t1.getName()); // Output: thread t1
Thread t2 = new Thread(r);
t2.setName("thread t2");
System.out.println(t2.getName()); // Output: thread t2
Note
Thread s long getId() method returns a unique long integer-based name for a thread. This number remains unchanged during the threads lifetime.
Getting a Threads Alive Status
You can determine if a thread is alive or dead by calling Thread s boolean isAlive() method. This method returns true when the thread is alive; otherwise, it returns false . A threads lifespan ranges from just before it is actually started from within the start() method (discussed later) to just after it leaves the run() method, at which point it dies. The following code fragment outputs the alive/dead status of a newly-created thread:
Thread t = new Thread(r);
System.out.println(t.isAlive()); // Output: false
Getting a Threads Execution State
A thread has an execution state that is identified by one of the Thread.State enums constants:
  • NEW : A thread that has not yet started is in this state.
  • RUNNABLE : A thread executing in the JVM is in this state.
  • BLOCKED : A thread that is blocked waiting for a monitor lock is in this state. (Ill discuss monitor locks in .)
  • WAITING : A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
  • TIMED_WAITING : A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
  • TERMINATED : A thread that has exited is in this state.
Thread lets an application determine a threads current state by providing the Thread.State getState() method, which is demonstrated here:
Thread t = new Thread(r);
System.out.println(t.getState()); // Output: NEW
Getting and Setting a Threads Priority
When a computer has enough processors and/or processor cores, the computers operating system assigns a separate thread to each processor or core so the threads execute simultaneously. When a computer doesnt have enough processors and/or cores, various threads must wait their turns to use the shared processors/cores.
Note
You can identify the number of processors and/or processor cores that are available to the JVM by calling the java.lang.Runtime classs int availableProcessors() method. The return value could change during JVM execution and is never smaller than 1.
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Java Threads and the Concurrency Utilities»

Look at similar books to Java Threads and the Concurrency Utilities. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Java Threads and the Concurrency Utilities»

Discussion, reviews of the book Java Threads and the Concurrency Utilities and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.