Multithreaded Programming

¡Supera tus tareas y exámenes ahora con Quizwiz!

RUNNABLE state

(ready->running) A thread executing in the JVM is in running state

Advantages of Java Multithreading

1) It doesn't block the user because threads are independent and you can perform multiple operations at same time. 2) You can perform many operations together so it saves time. 3) Threads are independent so it doesn't affect other threads if exception occur in a single thread.

Liveness

A concurrent application's ability to execute in a timely manner is known as its liveness.

A master process

A master process explicitly spawns sub processes, it makes sense to logically separate significant application functionalities.

A process,

A process is a program in execution. A process may be divided may be divided into a number of independent units known as threads.

A single process

A single process might contain multiple threads. all threads within a process share the same state, and the same space and they can communicate with each other directly because they share the same variables.

A thread

A thread is a dispatchable unit of work, threads are light-weight processes within a process

Livelock

A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further progress. However, the threads are not blocked — they are simply too busy responding to each other to resume work.

TERMINATED (dead) state

A thread that has exited is in this state.

BLOCKED state

A thread that is blocked waiting for a monitor lock is in this state. This can also occur when a thread performs an I/O operation and moves to next (runnable) state.

NEW state

A thread that is just instantiated is in new state. When a start() method is invoked, the thread moves to the ready state from which it is automatically moved to runnable state by the thread scheduler.

TIMED_WAITING (sleeping) state

A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.

WAITING state

A thread that is waiting indefinitely for another thread to perform a particular action is in this state.

Threads in java

Applications are typically divided into processes during the design phase. threads are object in the java language.

Deadlock

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Here's an example. Alphonse and Gaston are friends, and great believers in courtesy. A strict rule of courtesy is that when you bow to a friend, you must remain bowed until your friend has a chance to return the bow. Unfortunately, this rule does not account for the possibility that two friends might bow to each other at the same time. This example application, Deadlock, models this possibility:

Cooperative multitasking

Each application is responsible for relinquishing control of the processor to enable it to execute the other applications.

Thread Priorities

Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled.

MIN, MAX, AND NORM

Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5).

Multitasking of two or more

Multitasking of two or more processes is known as process based multitasking. multitasking of two or more threads is known as thread based multitasking.

Multithreading

One of the core features of java programming, it introduces the need for expressing concurrency to support simultaneous operations

Starvation and Livelock

Starvation and livelock are much less common a problem than deadlock, but are still problems that every designer of concurrent software is likely to encounter.

Starvation

Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads. For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.

A Synchronized Class Example

The class, SynchronizedRGB, defines objects that represent colors. Each object represents the color as three integers that stand for primary color values and a string that gives the name of the color.

Threads in Java

The life cycle of threads in java is similar to the life cycle of processes running in an operating system. A java thread moves from one thread to another, in six states.

Preemptive multitasking

The processor is responsible for executing each application in a certain amount of time called a timeslice.

Context structure

The processor moves all the information from the stack into a data structure called a context structure. When the processor wants to switch back to a previously executing thread, it transfers all the information from the context structure associated with the thread.

The need for synchronization

This form of communication is extremely efficient, but makes two kinds of errors possible: thread interference and memory consistency errors. The tool needed to prevent these errors is synchronization.

Java supports

Thread based multitasking its advantages include: - threads share the same address space - context switching between threads is normally inexpensive - communication between threads is normally inexpensive.

Synchronization

Threads communicate primarily by sharing access to fields and the objects reference fields refer to

Guarded Blocks

Threads often have to coordinate their actions. The most common coordination idiom is the guarded block. Such a block begins by polling a condition that must be true before the block can proceed. There are a number of steps to follow in order to do this correctly. https://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

Higher priority threads

Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and are very much platform dependent.

Link for multithreading on creating a thread

https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html

Contentation can be caused but steps can be taken to reduce it

https://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html .......look up on synchronization and the sub topics there.

More on multithreading

https://www.javatpoint.com/multithreading-in-java

Creating threads, the thread class and the runnable interface

https://www.tutorialspoint.com/java/java_multithreading.htm

Despite connecting threads

synchronization can introduce thread contention, which occurs when two or more threads try to access the same resource simultaneously and cause the Java runtime to execute one or more threads more slowly, or even suspend their execution

Context switching

the concept of context switching is integral to threading. a hardware timer is used by the processor to determine the end of the timeslice for each thread. The timer signals at the end of the timeslice and in turn the processor saves all information required for the current thread onto a stack.

Concept of multithreading

the concept of multithreading in a programming language refers to thread based multitasking.

Multitasking

the simultaneous occurrence of of several activities on a computer

Threads as objects

they can be created by using two mechanisms, 1. create a class that extends the standard thread class. 2. create a class that implements the standard runnable interface


Conjuntos de estudio relacionados

Dev. Psych Ch7: Middle Childhood Body&Mind Quiz

View Set

Nutrition ATI Practice Questions

View Set

LAW204_32_Business Law I_2020_21_TERM3

View Set

Chapter 37- The Experience of Loss, Grief, and Death

View Set

Ch.6 - Analyzing Cash Flow and Other Financial Information

View Set

Business Intel and Analytics - Chapter 1

View Set