What are the advantages or usage of threads?

Ans) Threads support concurrent operations. For example,-- Multiple requests by a client on a server can be handled as an individual client thread.-- Long computations or high-latency disk and network operations can be handled in the background without disturbing foreground computations or screen updates. Threads often result in simpler programs.-- In sequential programming, updating multiple displays normally requires a big while-loop that performs small parts of each display update. Unfortunately, this loop basically simulates an operating system...

Difference between Multithreading and Multiprocessing

Multi-threading refers to an application with multiple threads running within a process, while multi-processing refers to an application organised across multiple OS-level processes.A thread is a stream of instructions within a process. Each thread has its own instruction pointer, set of registers and stack memory. The virtual address space is process specific, or common to all threads within a process. So, data on the heap can be readily accessed by all threads, for good or ill.Multi-threading is a more "light weight" form of concurrency: there...

What is difference between thread and process?

 Ans) Differences between threads and processes are:- 1. Threads share the address space of the process that  created it; processes have their own address.2. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.3. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.4. Threads have almost no overhead; processes have considerable overhead.5. New threads are...

Questions Related to Thread

Q:What’s the difference between Thread and Runnable types? Ans: Java Thread controls the main path of execution in an application. When you invoke the Java Virtual Machine with the java command, it creates an implicit thread in which to execute the main method. The Thread class provides a mechanism for the first thread to start-up other threads to run in parallel with it.The Runnable interface defines a type of class that can be run by a thread. The only method it requires is run, which makes the interface very easy to to fulfil by extending existing...

ThreadGroup Class - Access Restriction Methods

The ThreadGroup class itself does not impose any access restrictions, such as allowing threads from one group to inspect or modify threads in a different group. Rather the Thread and ThreadGroup classes cooperate with security managers (subclasses of the SecurityManager class), which can impose access restrictions based on thread group membership.The Thread and ThreadGroup class both have a method, checkAccess, which calls the current security manager's checkAccess method. The security manager decides whether to allow the access based on the group...

Some Unique Guinness World Records

1,253 Smurfs gathered in the high street in the town of Castleblayney in County Monaghan, Ireland on July 18, 2008. (Guinness World Records) 35,310 Lego Star Wars Clone Troopers in the UK. The longest skis are 534 m long and were worn by 1,043 skiers in an event organized by Danske Bank on Drottninggatan in Örebro, Sweden, on 13 September 2008. World's biggest pickup truck - 2002 Surrey resident Sarwan Singh achieved a feat, which every Sikh is going to be proud of. He set a new Guinness...

ThreadGroup Class- Methods that Operate on all Threads within a Group

The ThreadGroup class has three methods that allow you to modify the current state of all the threads within that group:resumestopsuspendThese methods apply the appropriate state change to every thread in the thread group and its subgroups.Related Posts:- Java - ThreadGroup Class ThreadGroup Class - Collection Management Methods ThreadGroup Class - Methods that Operate on the Group ThreadGroup Class - Access Restriction Methods...

ThreadGroup Class - Methods that Operate on the Group

The ThreadGroup class supports several attributes that are set and retrieved from the group as a whole. These attributes include the maximum priority that any thread within the group can have, whether the group is a "daemon" group, the name of the group, and the parent of the group.The methods that get and set ThreadGroup attributes operate at the group level. They inspect or change the attribute on the ThreadGroup object, but do not affect any of the threads within the group. The following is a list of ThreadGroup methods that operate at the group...

ThreadGroup Class - Collection Management Methods

The ThreadGroup provides a set of methods that manage the threads and subgroups within the group and allow other objects to query the ThreadGroup for information about its contents. For example, you can call ThreadGroup's activeCount method to find out the number of active threads currently in the group. The activeCount method is often used with the enumerate method to get an array filled with references to all the active threads in a ThreadGroup. For example, the listCurrentThreads method in the following example fills an array with all of the...

Java - ThreadGroup Class

The ThreadGroup class manages groups of threads for Java applications. A ThreadGroup can contain any number of threads. The threads in a group are generally related in some way, such as who created them, what function they perform, or when they should be started and stopped.ThreadGroups can contain not only threads but also other ThreadGroups. The top-most thread group in a Java application is the thread group named main. You can create threads and thread groups in the main group. You can also...

Java - Thread Group

Every Java thread is a member of a thread group. Thread groups provide a mechanism for collecting multiple threads into a single object and manipulating those threads all at once, rather than individually. For example, you can start or suspend all the threads within a group with a single method call. Java thread groups are implemented by the ThreadGroup class in the java.lang package.The runtime system puts a thread into a thread group during thread construction. When you create a thread, you can either allow the runtime system to put the new...

Thread - Deadlock

There are situations when programs become deadlocked when each thread is waiting on a resource that cannot become available. The simplest form of deadlock is when two threads are each waiting on a resource that is locked by the other thread. Since each thread is waiting for the other thread to relinquish a lock, they both remain waiting forever in the Blocked-for-lock-acquisition state. The threads are said to be deadlocked. Thread t1 at tries to synchronize first on string o1 and then on string o2. The thread t2 does the opposite. It synchronizes...

Java - Thread Joining

A thread invokes the join() method on another thread in order to wait for the other thread to complete its execution.Consider a thread t1 invokes the method join() on a thread t2. The join() call has no effect if thread t2 has already completed. If thread t2 is still alive, then thread t1 transits to the Blocked-for-join-completion state. Below is a program showing how threads invoke the overloaded thread join method. public class ThreadJoinDemo { public static void main(String[] args) { Thread t1 = new Thread("T1"); ...

Java - The notify() and wait() Methods

The get() and put() methods in the CubbyHole object both make use of the notify() and wait() methods to coordinate getting and putting values into the CubbyHole. Both notify() and wait() are members of the java.lang.Object class.The notify() and wait() methods can only be called from a synchronized method. The notify() method The get() method calls notify() as the last thing it does (besides return). The notify() method chooses one thread that is waiting on the monitor held by the current thread and wakes it up. Typically, the waiting thread will...

Java - Thread Scheduler

Schedulers in JVM implementations usually employ one of the two following strategies: Preemptive scheduling If a thread with a higher priority than all other Runnable threads becomes Runnable, the scheduler will preempt the running thread (is moved to the runnable state) and choose the new higher priority thread for execution. Time-Slicing or Round-Robin scheduling A running thread is allowed to execute for a fixed length of time (a time slot it’s assigned to), after which it moves to the Ready-to-run state (runnable) to await its turn to run...

Java - Thread Synchronized Blocks

The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object. A compile-time error occurs if the expression produces a value of any primitive type. If execution of the block completes normally, then the lock is released. If execution of the block completes abruptly, then the lock is released. A thread can hold more than one lock at a time. Synchronized statements can be nested. Synchronized statements with identical expressions can be nested. The expression must evaluate to a non-null reference...

Java - Thread Synchronized Method

The Tutorial want to explain you a code that help you in understanding Java Method Synchronized. We have a class Synchronized Method. In order to make a method Synchronized, we add synchronized keyword to the method. The synchronized int get Count ( ) method return you the count of thread executed in a code. The static void print(String ms) includes a Thread.currentThread ( ).get Name( ) return you the name of the current thread. The print ln print the thread Name. Inside the main method, The...

Java - Thread Synchronization

With respect to multithreading, Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access a particular resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object’s value. Synchronization prevents such type of data corruption which may otherwise lead to dirty reads and significant errors. Generally critical sections of the...

Java - Thread Priorities

Every thread has a priority assigned to it. Thread priority in Java has the effect of controlling the flow of program. Here I will describe Java thread priorities and also share same code for defining priorities to Java threads. Since with the use of multi threading, there is no defined order in which the threads will execute. But Java provides few mechanisms by using which one can control the execution of multiple threads to some extent and assign thread priorities to them. In Java, threads can have three level of priorities viz:1) Min Priority...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More