Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Java Threads: DR T Nyathi

Lambda expressions allow Java code to be treated as data and passed around or assigned to variables and used to implement functional interfaces. They help simplify code by providing implementations for interfaces with only one method. Multithreading allows multiple threads to run simultaneously, improving performance over single threaded processing. Threads share memory and context switching is faster than processes. The Callable interface is similar to Runnable but allows returning values from asynchronous operations.

Uploaded by

jabule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Java Threads: DR T Nyathi

Lambda expressions allow Java code to be treated as data and passed around or assigned to variables and used to implement functional interfaces. They help simplify code by providing implementations for interfaces with only one method. Multithreading allows multiple threads to run simultaneously, improving performance over single threaded processing. Threads share memory and context switching is faster than processes. The Callable interface is similar to Runnable but allows returning values from asynchronous operations.

Uploaded by

jabule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Java Threads

Dr T Nyathi
Java Lambda Expressions

● Lambda expression is a new and important feature of Java It


helps to iterate, filter and extract data from collection.
● The Lambda expression is used to provide the implementation
of an interface which has functional interface. It saves a lot of
code.
● Java lambda expression is treated as a function, so compiler
does not create .class file.
● Lambda expression provides implementation of functional
interface.
● An interface which has only one abstract method is called
functional interface.
Lambda

Lambda expression is consisted of three components.

1) Argument-list: It can be empty or non-empty as


well.
2) Arrow-token: It is used to link arguments-list and
body of expression.
3) Body: It contains expressions and statements for
lambda expression.
Multithreading in Java

● Multithreading in Java is a process of executing multiple


threads simultaneously.
● A thread is a lightweight sub-process, the smallest unit of
processing. Multiprocessing and multithreading, both are used
to achieve multitasking.
● However, we use multithreading than multiprocessing because
threads use a shared memory area. They don't allocate
separate memory area so saves memory, and
context-switching between the threads takes less time than
process.
● Java Multithreading is mostly used in games, animation, etc.
Advantages of Multithreading

1) It doesn't block the user because threads are


independent and you can perform multiple operations
at the 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 an exception occurs in a single thread.
Concurrency (Multithreading)
● Concurrency is the ability to run several programs or several parts of a
program in parallel.
● If a time consuming task can be performed asynchronously or in parallel,
this improves the throughput and the interactivity of the program.
(example ?)
● A modern computer has several CPU’s or several cores within one CPU.
● The ability to leverage these multi-cores can be the key for a successful
high-volume application.
Process vs Threads
● A process runs independently and isolated of other processes. It cannot
directly access shared data in other processes.(OS controlled)
● A thread is a so called lightweight process. It has its own call stack, but can
access shared data of other threads in the same process.
● Every thread has its own memory cache. If a thread reads shared data, it
stores this data in its own memory cache.
● A thread can re-read the shared data.
● A Java application runs by default in one process. Within a Java application
you work with several threads to achieve parallel processing or asynchronous
behavior.
● Threads have their own call stack, but can also access shared data. Therefore
you have two basic problems, visibility and access problems.

● A visibility problem occurs if thread A reads shared data which is later


changed by thread B and thread A is unaware of this change.

● An access problem can occur if several threads access and change the same
shared data at the same time.
Creating a Java thread - Runnable
Creating a Java thread - Thread
Creating a Java thread - lambda
Callable

A Callable interface defined in java.util.concurrent package. An


object of Callable returns a computed result done by a thread in
contrast to a Runnable interface that can only run the thread. The
Callable object returns Future object that provides methods to
monitor the progress of a task executed by a thread. An object of
the Future used to check the status of a Callable interface and
retrieves the result from Callable once the thread has done.
https://howtodoinjava.com/java/multi-threading/java-callable-future
-example/
Runnable and Callable

● Runnable and Callable both functional interface. Classes which are


implementing these interfaces are designed to be executed by another
thread.
● Thread can be started with Ruunable and they are two ways to start a
new thread: one is by subclassing Thread class and another is
implementing Runnable interface.
● Thread class does not have constructor for callable so we should use
ExecutorService class for executing thread.

You might also like