What is Multithreading? Explain how Java supports programming for multitasking environment. - A thread is one of the processes of an application. In multithreading, an OS allows different threads of an application to run in parallel.
The multitasking environment can execute in two ways: a. Process based multitasking b. Thread based multitasking
- Process based multitasking – Process based multitasking is when two different processes, running at different locations, execute simultaneously. E.g.: A user can write some text using editor and simultaneously play music.
- Thread based multitasking – A thread is the smallest unit of a dispatchable code.
- Two different tasks like printing a document and editing it simultaneously can be done in a thread based multitasking environment.
- Threads can be implemented in Java using the Thread Class.
- The threading concept says that the different blocks of the same program can be executed concurrently. Explain how to create thread in Java. We can create thread in two ways in Java:
i.) Extend the java.lang.Thread class ii.) Implement the java.lang.Runnable interface
Using java.lang.Thread Create a class that extends the thread class. The object thus created is automatically bound to the thread. We can call object's start() to start the thread.
Using java.lang.Runnable Create a class that implements Runnable interface. The class should override run() method. Create the thread and supply it the object created with a run() method.What are native operating system threads? Native operating system threads are provided by the operating system. These threads are the host to any Java application. OS threads make the computers run many applications / programs simultaneously on the same CPU. The threads run without clashing over the system resources usage or more time consuming for one program. These threads are optimized pertaining to microprocessor architecture and run much faster than the Java green thread processing.What is a green thread? A green thread is an operation mode for the JVM, in which the entire code is executed in a single thread of OS. JVM manages multi threads internally in case a java application has concurrent threads, without using OS threads.
Green thread implementation is deprecated in the recent application development. The reason is significant processing over head for JVM for the purpose of keeping track of all the thread states and swapping between them. What is a working thread? A working thread is a key part of a design pattern which allocates one thread per thread execution. The thread may return a thread pool for later use, once the task is completed. In this scenario, a thread may execute some arbitrary tasks. These are passed as a Runnable method argument, typically execute(Runnable). These tasks are queued up until a thread host is available in order to execute them.What is the SwingUtilities.invokeLater(Runnable) method for? The method SwingUtilities.invokeLater(Runnable) executes a new runnable thread from a Swing application. The normal sequence of dispatching events from GUI will not be disturbed. This method places all the runnable objects in a queue of AWT events which are to be processed and returns immediately. The run() method of Runnable object will be called after reaching to the beginning of the queue. The invokeLater(Runnable) method ensures the necessary updates to the UI can occur immediately. While the runnable thread executing, the invokeLater() method may be used to start a process of clicking a button and also make a change to the UI at once. What is the volatile modifier for? The volatile access modifier is used to find out the variables which are not optimised by the JVM. This modifier is used for the variables which may be accessed by various threads and modifies and for changing the values without synchronization.
|