Learn Java: Threads

Topics: All about Threads'

Browse all answers:

What is the difference between processes and threads? 
Explain different ways of creating a thread?
Write example of thread?
Write thread program using runnable interface?
What is a runnable in thread?
What is the use of Running in threads?
Need of Waiting in thread?
What is the use of Sleeping in threads?
Explain Blocked on I/O stage in threads?
Explain Blocked on synchronization stage in threads?
Explain Dead in threads?
What is the difference between yield and sleeping? What is the difference between the methods sleep( ) and wait( )? 
Why is locking of a method or block of code for thread safety is called "synchronized" and not "lock" or "locked"?
How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply? What is the difference between synchronized method and synchronized block? 
Why synchronization is important?  
What is a ThreadLocal class?  
What is a daemon thread?  
How can threads communicate with each other? How would you implement a producer (one thread) and a consumer (another thread) passing data (via stack)? 
Which of the following is true?a) wait( ), notify( ) ,notifyall( ) are defined as final & can be called only from within a synchronized methodb) Among wait( ), notify( ), notifyall( ) the wait() method only throws IOExceptionc) wait( ),notify( ),notifyall( ) & sleep () are methods of object class
What are some of the threads related problems and what causes those problems?
What happens if you call the run( ) method directly instead of via the start method?
What happens if you restart a thread that has already started?
Write a multi-threaded Java program in which, one thread generates odd numbers and write to a pipe and the second thread generates even numbers and write to another pipe, and a third thread receives the numbers from both the pipes and evaluates if the sum is multiples of 5?
Explain   Class Thread in awt?
Explain method public Thread(ThreadGroup group,              Runnable target,              String name) in threads?
Explain method public Thread(ThreadGroup group,              Runnable target,              String name,              long stackSize) in thredas?
Explain method currentThread in threads?
Explain method public static void yield() in threads?
Explain method public static void sleep(long millis)  in threads?                  throws InterruptedException
Explain  method public static void sleep(long millis,                         int nanos)                  throws InterruptedException  in threads?
Explain method start in java?
Explain method run in java?
Explain method stop in java?
Explain method interrupt in java?
Explain method interrupted in threads?
Explain method isInterrupted in threads?
Explain method destroy in threads?
Explain method  isAlive in threads?
Explain method suspend in threads?
Explain method resume in threads?
Explain method getPriority in threads?
Explain method setName in threads?
Explain method getName in threads?
Explain method getThreadGroup in threads?
Explain method activeCount in threads?
Explain method enumerate in threads?
Explain method countStackFrames in threads?
Explain method  join in  threads
Explain method join?
Explain method dumpStack in threads?
Explain method setDaemon in threads?
Explain method isDaemon in threads?
Explain method checkAccess in threads?
Explain method toString in threads?
Explain method getContextClassLoader in threads?
Explain method setContextClassLoader in threads?
Explain method holdsLock in threads?
Explain method getStackTrace in threads?
Explain method getAllStackTraces in threads?
Explain methods getId in threads?
Explain method getState in threads?
Explain method setDefaultUncaughtExceptionHandler in threads?
Explain method getDefaultUncaughtExceptionHandler in threads?
Explain method getUncaughtExceptionHandler in threads?
Explain method setUncaughtExceptionHandler in threads?
Explain field  MAX_PRIORITY  in threads?
Explain field MIN_PRIORITY in treads ?
Explain field NORM_PRIORITY  in threads?
Explain Interface Thread.UncaughtExceptionHandler in threads?
What is the Fork/Join framework (JSR 166y)?
What is the purpose of the CopyOnWriteArrayList and CopyOnWriteArraySet collections?
What is the primary difference between pessimistic and optimistic concurrency control?
How do I configure a default handler for exceptions that happen in all threads? 
How can I get the number of active Groups within a ThreadGroup?
What is a ThreadGroup? 
How can a Thread preempt another Thread?
What is a Thread identifer and how can I obtain it?
How can I create a thread pool? 
How can I tell the difference between wait(timeout) timing out and actually being notified?
What purpose does the TimeUnit class have?
How can i tell what state a thread is in?
How can I determine if a thread is alive? 
How can I create a daemon thread? 
How do I configure a default handler for exceptions that happen in my threads?
What happens if two threads perform a get of one hashmap at the same time? 
Is it possible to wake up a sleeping thread? 
Thread synchronization seems to work using a queue -- that the waiting threads will acquire the released lock in first-in, first-out order. Is this behavior reliable? 
How do I capture an exception stack trace and put it into a string? 
What is double-checked locking? Does it work? 
What are the threading options available when using hotspot on solaris? What are the performance implications? and defaults for various JVM versions?
What does it mean to lock an object? 
How do I reset a scheduled task so that it stops and then is rescheduled for the next cycle? 
Is Vector's clone method thread-safe? 
Do I have to synchronize read-only access to a text file? 
How to stop a thread which is waiting for a client socket connection? 
Can we fork another process from Java? 
How soon after calling start() will the run() method be executed? 
In case of images how does thread spawning work? Can I stop the AWT from creating a thread for the image while loading? 
What is piped I/O used for?
How do I exacute a block of code after the first of two threads finishes, no matter which one finishes first?
How do we stop the AWT Thread? Our Swing application will not terminate. 
How can i check the status of a java Thread using JVMDI? (for deadlocks etc.)
How do I stop a daemon thread gracefully when the server shuts down? 
How can I synchronize across two different classes? 
I am looking for a monitoring tool that will give me the status of the threads inside Weblogic?
If an object is referenced by a local variable -- is it safe from concurrency issues?
Is there some way to implement multi-tasking in Java (like "fork()" on UNIX)?
When returning from a wait(long), can I tell whether my object was signaled or the wait timed out? 
If an event handler throws an exception which it does not itself catch, there is a crash in the current EventQueue DispatchThread. 
How can I find out how much CPU is taken by each JVM thread ? 
How do I pass a variable into a thread that can be accessed from the run() method? 
What is the difference between sleep and yield?
Why do I have to call System.exit() from my main method?
How can I discover the current process ID from Java? 
Is there any method by which I can join() to a group of threads instead of a single one? 
What is the difference between "Green Threads" and "Native Threads" ? 
Why does Java prohibit the use of "synchronized" on a constructor? 
Is there anything special that needs to be done in order to get the JVM to work efficiently with multiple CPUs on a Windows NT (4.0) machine? 
As Solaris has many-to-many threading model/two level threading model, can you tell me how the user thread is mapped to a LWPs exactly? What is the algorithm that is is being followed? Is there any limit (that these many no of threads can only be mapped to one LWP & vice versa)?
How does SunOS 5.6(solaris) handle Java threads on multiple CPUs? 
In my main thread I create and start a new Thread(B). From within the run method of Thread B I invoke a public method of my Main class. This method is currently getting executed in Thread B instead I want that method to get executed in Thread Main. Is this possible?
How do I implement a timeout using Java threads? Specifically, I want to connect to one of two servers. If the first one fails after a certain amount of time, I want to try the second one. How do I interrupt the first connection attempt? 
How can I create a pool of threads for use in my program?
Does Java keep a count of the number of notifys for a given object? That is, if notify() is called three times on on object A, then will three waits at a later time proceed immediately? Also, if there are three waits on A then at a later point three notifies on A will resume all of three of them?
How can I find out how many threads are running in the JVM?
I have a static method where I pass some parameters and it does db manipulations, returning some resultset. Do I need to synchronize this method to make it thread safe? It does not use any other variables. It only uses local variables passed as method parameters.
How does Linux handle Java thread priorities?
If an exception is thrown inside a synchronized block will the lock on the object that was synchronized be automatically released? 
How can I determine if a wait(timeout) timed out
How can I determine if a wait(timeout) timed out or was notify()'ed? I'm thinking of the case where multiple threads are waiting and notifyAll() is NOT used.
What's wrong with Java threads?
Why is Thread.run() declared public? Shouldn't it be declared protected, as only the Thread superclass can invoke it?
How does JavaMail handle thread management? Looks like by default, most of the threads created by JavaMail while fetching the messages from a Store/Folder object are not being released automatically. Do I need to explicitely invoke any method so that these threads get released?
What is the difference between multithreading and multitasking? What about multiprogramming? Multiprocessing?
When exactly is the AWT thread started?
Does the main thread get terminated after returning from main(), even after spawning one or more threads? 
What is InterruptedException? Why do we need to catch it when calling Thread.sleep()? 
What is the mechanism of Thread Priority & Thread Scheduler? How do Threads with various priority levels behave in time-sliced and non-time-sliced environments?
what threads are initialized when you start an application? (I heard six. What do they do?)
Is there a limit to the number of threads that can be spawned in a program?
What is deadlock? How can I eliminate it?
When and why is IllegalMonitorStateException thrown? 
What is the difference between threads and interrupts ?
How do you write a Thread-Safe Singleton?
How does one read a thread dump? (Especially the first line, '"47" (TID:0x1780818, sys_thread_t:0x9b4780, state:CW, native ID:0xd4) prio=5', and the Monitor Cache Dump.)