Tuesday, 14 February 2012

How does the run() method in Runnable work? | Java Threads

It may help to think of the run method like the main method in standard single threaded applications. The run method is a standard entry point to run or execute a class. The run method is normally only executed in the context of an independent Thread, but is a normal method in all other respects.

What is the difference between Thread and Runnable types? | Java Threads

A 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.

Why are there separate wait and sleep methods? | Java Threads

The static Thread.sleep(long) method maintains control of thread execution but delays the next action until the sleep time expires. The wait method gives up control over thread execution indefinitely so that other threads can run.

Why are wait(), notify() and notifyall() methods defined in the Object class? | Java Threads

These methods are detailed on the Java Software Development Kit JavaDoc page for the Object class, they are to implement threaded programming for all subclasses of Object.

What is threaded programming and when is it used? | Core Java

     Threaded programming is normally used when a program is required to do more than one task at the same time. Threading is often used in applications with graphical user interfaces; a new thread may be created to do some processor-intensive work. while the main thread keeps the interface responsive to human interaction.
 
     The Java programming language has threaded programming facilities built in, so it is relatively easy to create threaded programs. However, multi-threaded programs introduce a degree of complexity that is not justified for most simple command line applications.

38 Java Threads Interview Questions and Answers freshers pdf free download

This page contains free download of Java Threads interview questions and answers in pdf format
1. What is threaded programming and when is it used?
2. Why are wait(), notify() and notifyall() methods defined in the Object class?
3. Why are there separate wait and sleep methods?
4. What is the difference between Thread and Runnable types?
5. How does the run() method in Runnable work?
6. A Thread is runnable, how does that work?
7. Why not override Thread to make a Runnable?
8. What is the difference between a threads start() and run() methods?
9. Can I implement my own start() method?
10. Do I need to use synchronized on setValue(int)?
11. How do I create a Runnable with inheritance?
12. What are three ways in which a thread can enter the waiting state?
13. What is the difference between yielding and sleeping?
14. How to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state?
15. What is mutual exclusion? How can you take care of mutual exclusion using Java threads?
16. What is the difference between preemptive scheduling and time slicing?
17. What invokes a threads run() method?
18. What is the purpose of the wait(), notify(), and notifyAll() methods?
19. What is thread? What are the high-level thread states?
20. What is deadlock?
21. How does multithreading take place on a computer with a single CPU?
22. What are synchronized methods and synchronized statements?
23. Can Java object be locked down for exclusive use by a given thread?
24. What is the difference between the methods sleep() and wait()?
25. What is the difference between process and thread?
26. What is daemon thread and which method is used to create the daemon thread?
27. What do you understand by Synchronization?(or) What Is Synchronization?
28. When you will synchronize a piece of your code?
29. Why would you use a synchronized block vs. synchronized method?
30. What is an objects lock and which objects have locks?
31. What state does a thread enter when it terminates its processing?
32. How would you implement a thread pool?
33. Is there a separate stack for each thread in Java?
34. What is the SwingUtilities.invokeLater(Runnable) method for?
35. What is the volatile modifier for?
36. Which class is the wait() method defined in?
37. What is a green thread?
38. What is a working thread?

Why would you use SwingUtilities.invokeAndWait or SwingUtilities.invokeLater? | Core Java

I want to update a Swing component but I’m not in a callback. If I want the update to happen immediately (perhaps for a progress bar component) then I’d use invokeAndWait. If I don’t care when the update occurs, I’d use invokeLater.