PROGRAMMING IN JAVA - LECTURE -52- INTRODUCTION TO THREAD - BCA- S3.

217 Просмотры
Издатель
INTRODUCTION TO THREAD
• Process and Thread are two basic units of Java program execution.
• Process: A process is a self contained execution environment and it can be seen as a program or application.
• Thread: It can be called lightweight process
• Thread requires less resources to create and exists in the process
• Thread shares the process resources
MULTITHREADING
• Multithreading in java is a process of executing multiple processes simultaneously
• A program is divided into two or more subprograms, which can be implemented at the same time in parallel
. • Multiprocessing and multithreading, both are used to achieve multitasking.
• Java Multithreading is mostly used in games, animation etc.
ADVANTAGE:
• It doesn't block the user
• can perform many operations together so it saves time.
• Threads are independent so it doesn't affect other threads
Single-threaded program
A thread is similar to a program that has a single flow of control. It has a beginning, a body, and an end, and executes commands sequentially. In fact, all main programs in our earlier examples can be called single-threaded programs.
Once initiated by the main thread, the threads A, B, and C run concurrently and share the resources jointly. It is like people living in joint families and sharing certain resources among all of them. The ability of a language to support multithreads is referred to as concurrency. Since threads in Java are subprograms of a main application program and share the same memory space, they are known as lightweight threads or lightweight processes.
Multithreading is a powerful programming tool that makes Java distinctly different from its fellow programming languages. Multithreading is useful in a number of ways. It enables programmers to do multiple things at one time. They can divide a long program into threads and execute them in parallel. For example, we can send tasks such as printing into the background and continue to perform some other task in the foreground. This approach would considerably improve the speed of our programs.
Multi-threading Multitasking
It is a programming language concept in which a program or process is divided into two or more subprograms that are executed at the same time in parallel. It is an operating system concept in which multiple tasks are performed simultaneously.
It is highly efficient. It is less efficient than Multi-threading.
It supports execution of multiple parts of a single program simultaneously. It supports execution of multiple programs simultaneously.
The processor has to switch between different parts or threads of a program. The processor has to switch between different programs or processes.
A thread is the smallest unit in multi-threading. A program or process is the smallest unit in multitasking.
It is cost effective in case of context switching. It is expensive in case of context switching.
It helps in developing efficient programs. It helps in developing efficient operating systems.
Категория
Язык программирования Java
Комментариев нет.