`
iloveflower
  • 浏览: 79861 次
社区版块
存档分类
最新评论
  • iloveflower: 呵呵。好好学习。。。。。。。。。。。。
    java 读书
  • Eric.Yan: 看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
    java 读书

18mutithread interview question

 
阅读更多
Most of the Core Java interview questions are derived from Multi-Threading & Collections framework. This post is a collection of some good questions in threads area which I came across and thought of taking a note for referring anytime in future.


1.What is synchronization in respect to multi-threading in Java?
With respect to multi-threading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one Java thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to erroneous behavior or program.
1.Explain different way of using thread?
A Java thread could be implemented by using Runnable interface or by extending the Thread class. The Runnable is more advantageous, when you are going for multiple inheritance.
1.What is the difference between Thread.start() & Thread.run() method?
Thread.start() method (native method) of Thread class actually does the job of running the Thread.run() method in a thread. If we directly call Thread.run() method it will executed in same thread, so does not solve the purpose of creating a new thread.
1.Why do we need run() & start() method both. Can we achieve it with only run method?
We need run() & start() method both because JVM needs to create a separate thread which can not be differentiated from a normal method call. So this job is done by start method native implementation which has to be explicitly called. Another advantage of having these two methods is we can have any object run as a thread if it implements Runnable interface. This is to avoid Java’s multiple inheritance problems which will make it difficult to inherit another class with Thread.
1.What is ThreadLocal class? How can it be used?
Below are some key points about ThreadLocal variables ◦A thread-local variable effectively provides a separate copy of its value for each thread that uses it.
◦ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread
◦In case when multiple threads access a ThreadLocal instance, separate copy of Threadlocal variable is maintained for each thread.
◦Common use is seen in DAO pattern where the DAO class can be singleton but the Database connection can be maintained separately for each thread. (Per Thread Singleton)
ThreadLocal variable are difficult to understand and I have found below reference links very useful in getting better understanding on them ◦Good article on ThreadLocal on IBM DeveloperWorks
◦Managing data : Good example
◦Refer Java API Docs

1.When InvalidMonitorStateException is thrown? Why?
This exception is thrown when you try to call wait()/notify()/notifyAll() any of these methods for an Object from a point in your program where u are NOT having a lock on that object.(i.e. u r not executing any synchronized block/method of that object and still trying to call wait()/notify()/notifyAll()) wait(), notify() and notifyAll() all throw IllegalMonitorStateException. since This exception is a subclass of RuntimeException so we r not bound to catch it (although u may if u want to). and being a RuntimeException this exception is not mentioned in the signature of wait(), notify(), notifyAll() methods.
1.What is the difference between sleep(), suspend() and wait() ?
Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has aquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread. Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread. t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone. object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist. Wait is called on a object, not a thread; we call this object the "lock object." Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.
1.What happens when I make a static method as synchronized?
Synchronized static methods have a lock on the class "Class", so when a thread enters a synchronized static method, the class itself gets locked by the thread monitor and no other thread can enter any static synchronized methods on that class. This is unlike instance methods, as multiple threads can access "same synchronized instance methods" at same time for different instances.
1.Can a thread call a non-synchronized instance method of an Object when a synchronized method is being executed ?
Yes, a Non synchronized method can always be called without any problem. In fact Java does not do any check for a non-synchronized method. The Lock object check is performed only for synchronized methods/blocks. In case the method is not declared synchronized Jave will call even if you are playing with shared data. So you have to be careful while doing such thing. The decision of declaring a method as synchronized has to be based on critical section access. If your method does not access a critical section (shared resource or data structure) it need not be declared synchronized. Below is the example which demonstrates this, The Common class has two methods synchronizedMethod1() and method1() MyThread class is calling both the methods in separate threads,


view plaincopy to clipboardprint?
01.public class Common { 
02. 
03.public synchronized void synchronizedMethod1() { 
04.System.out.println("synchronizedMethod1 called"); 
05.try { 
06.Thread.sleep(1000); 
07.} catch (InterruptedException e) { 
08.e.printStackTrace(); 
09.} 
10.System.out.println("synchronizedMethod1 done"); 
11.} 
12.public void method1() { 
13.System.out.println("Method 1 called"); 
14.try { 
15.Thread.sleep(1000); 
16.} catch (InterruptedException e) { 
17.e.printStackTrace(); 
18.} 
19.System.out.println("Method 1 done"); 
20.} 
21.} 



view plaincopy to clipboardprint?
01.public class MyThread extends Thread { 
02.private int id = 0; 
03.private Common common; 
04. 
05.public MyThread(String name, int no, Common object) { 
06.super(name); 
07.common = object; 
08.id = no; 
09.} 
10. 
11.public void run() { 
12.System.out.println("Running Thread" + this.getName()); 
13.try { 
14.if (id == 0) { 
15.common.synchronizedMethod1(); 
16.} else { 
17.common.method1(); 
18.} 
19.} catch (Exception e) { 
20.e.printStackTrace(); 
21.} 
22.} 
23. 
24.public static void main(String[] args) { 
25.Common c = new Common(); 
26.MyThread t1 = new MyThread("MyThread-1", 0, c); 
27.MyThread t2 = new MyThread("MyThread-2", 1, c); 
28.t1.start(); 
29.t2.start(); 
30.} 
31.} 
Here is the output of the program


view plaincopy to clipboardprint?
01.Running ThreadMyThread-1 
02.synchronizedMethod1 called 
03.Running ThreadMyThread-2 
04.Method 1 called 
05.synchronizedMethod1 done 
06.Method 1 done 
This shows that method1() - is called even though the synchronizedMethod1() was in execution.
1.Can two threads call two different synchronized instance methods of an Object?
No. If a object has synchronized instance methods then the Object itself is used a lock object for controlling the synchronization. Therefore all other instance methods need to wait until previous method call is completed. See the below sample code which demonstrate it very clearly. The Class Common has 2 methods called synchronizedMethod1() and synchronizedMethod2() MyThread class is calling both the methods


view plaincopy to clipboardprint?
01.public class Common { 
02.public synchronized void synchronizedMethod1() { 
03.System.out.println("synchronizedMethod1 called"); 
04.try { 
05.Thread.sleep(1000); 
06.} catch (InterruptedException e) { 
07.e.printStackTrace(); 
08.} 
09.System.out.println("synchronizedMethod1 done"); 
10.} 
11. 
12.public synchronized void synchronizedMethod2() { 
13.System.out.println("synchronizedMethod2 called"); 
14.try { 
15.Thread.sleep(1000); 
16.} catch (InterruptedException e) { 
17.e.printStackTrace(); 
18.} 
19.System.out.println("synchronizedMethod2 done"); 
20.} 
21.} 



view plaincopy to clipboardprint?
01.public class MyThread extends Thread { 
02.private int id = 0; 
03.private Common common; 
04. 
05.public MyThread(String name, int no, Common object) { 
06.super(name); 
07.common = object; 
08.id = no; 
09.} 
10. 
11.public void run() { 
12.System.out.println("Running Thread" + this.getName()); 
13.try { 
14.if (id == 0) { 
15.common.synchronizedMethod1(); 
16.} else { 
17.common.synchronizedMethod2(); 
18.} 
19.} catch (Exception e) { 
20.e.printStackTrace(); 
21.} 
22.} 
23. 
24.public static void main(String[] args) { 
25.Common c = new Common(); 
26.MyThread t1 = new MyThread("MyThread-1", 0, c); 
27.MyThread t2 = new MyThread("MyThread-2", 1, c); 
28.t1.start(); 
29.t2.start(); 
30.} 
31.} 

1.What is a deadlock?
Deadlock is a situation where two or more threads are blocked forever, waiting for each other. This may occur when two threads, each having a lock on one resource, attempt to acquire a lock on the other's resource. Each thread would wait indefinitely for the other to release the lock, unless one of the user processes is terminated. In terms of Java API, thread deadlock can occur in following conditions: ◦When two threads call Thread.join() on each other.
◦When two threads use nested synchronized blocks to lock two objects and the blocks lock the same objects in different order.

1.What is Starvation? and What is a Livelock?
Starvation and livelock are much less common a problem than deadlock, but are still problems that every designer of concurrent software is likely to encounter.
LiveLock
Livelock occurs when all threads are blocked, or are otherwise unable to proceed due to unavailability of required resources, and the non-existence of any unblocked thread to make those resources available. In terms of Java API, thread livelock can occur in following conditions: ◦When all the threads in a program execute Object.wait(0) on an object with zero parameter. The program is live-locked and cannot proceed until one or more threads call Object.notify() or Object.notifyAll() on the relevant objects. Because all the threads are blocked, neither call can be made.
◦When all the threads in a program are stuck in infinite loops.

Starvation
Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. This happens when shared resources are made unavailable for long periods by "greedy" threads. For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked. Starvation occurs when one thread cannot access the CPU because one or more other threads are monopolizing the CPU. In Java, thread starvation can be caused by setting thread priorities inappropriately. A lower-priority thread can be starved by higher-priority threads if the higher-priority threads do not yield control of the CPU from time to time.
1.How to find a deadlock has occurred in Java? How to detect a Deadlock in Java?
Earlier versions of Java had no mechanism to handle/detect deadlock. Since JDK 1.5 there are some powerful methods added in the java.lang.management package to diagnose and detect deadlocks. The java.lang.management.ThreadMXBean interface is management interface for the thread system of the Java virtual machine. It has two methods which can leveraged to detect deadlock in a Java application. ◦findMonitorDeadlockedThreads() - This method can be used to detect cycles of threads that are in deadlock waiting to acquire object monitors. It returns an array of thread IDs that are deadlocked waiting on monitor.
◦findDeadlockedThreads() - It returns an array of thread IDs that are deadlocked waiting on monitor or ownable synchronizers.

1.What is immutable object? How does it help in writing concurrent application?
An object is considered immutable if its state cannot change after it is constructed. Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code. Immutable objects are particularly useful in concurrent applications. Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state. Examples of immutable objects from the JDK include String and Integer. Immutable objects greatly simplify your multi threaded program, since they are ◦Simple to construct, test, and use.
◦Automatically thread-safe and have no synchronization issues.
To create a object immutable You need to make the class final and all its member final so that once objects gets crated no one can modify its state. You can achieve same functionality by making member as non final but private and not modifying them except in constructor.
1.How will you take thread dump in Java? How will you analyze Thread dump?
A Thread Dump is a complete list of active threads. A java thread dump is a way of finding out what each thread in the JVM is doing at a particular point of time. This is especially useful when your java application seems to have some performance issues. Thread dump will help you to find out which thread is causing this. There are several ways to take thread dumps from a JVM. It is highly recommended to take more than 1 thread dump and analyze the results based on it. Follow below steps to take thread dump of a java process ◦Step 1

On UNIX, Linux and Mac OSX Environment run below command:

ps -el | grep java

On Windows:

Press Ctrl+Shift+Esc to open the task manager and find the PID of the java process


◦ Step 2:

Use jstack command to print the Java stack traces for a given Java process PID

jstack [PID]

More details of jstack command can be found here : JSTACK Command Manual



1.What is a thread leak? What does it mean in Java?
Thread leak is when a application does not release references to a thread object properly. Due to this some Threads do not get garbage collected and the number of unused threads grow with time. Thread leak can often cause serious issues on a Java application since over a period of time too many threads will be created but not released and may cause applications to respond slow or hang.
1.How can I trace whether the application has a thread leak?
If an application has thread leak then with time it will have too many unused threads. Try to find out what type of threads is leaking out. This can be done using following ways ◦Give unique and descriptive names to the threads created in application. - Add log entry in all thread at various entry and exit points in threads.
◦Change debugging config levels (debug, info, error etc) and analyze log messages.
◦When you find the class that is leaking out threads check how new threads are instantiated and how they're closed.
◦Make sure the thread is Guaranteed to close properly by doing following - Handling all Exceptions properly.
◦Make sure the thread is Guaranteed to close properly by doing following ■Handling all Exceptions properly.
■releasing all resources (e.g. connections, files etc) before it closes.


1.What is thread pool? Why should we use thread pools?
A thread pool is a collection of threads on which task can be scheduled. Instead of creating a new thread for each task, you can have one of the threads from the thread pool pulled out of the pool and assigned to the task. When the thread is finished with the task, it adds itself back to the pool and waits for another assignment. One common type of thread pool is the fixed thread pool. This type of pool always has a specified number of threads running; if a thread is somehow terminated while it is still in use, it is automatically replaced with a new thread. Below are key reasons to use a Thread Pool ◦ Using thread pools minimizes the JVM overhead due to thread creation. Thread objects use a significant amount of memory, and in a large-scale application, allocating and de-allocating many thread objects creates a significant memory management overhead.
◦You have control over the maximum number of tasks that are being processed in parallel (= number of threads in the pool).
Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks.
分享到:
评论

相关推荐

    MutiThread.rar

    首先,让我们关注标题中的“MutiThread.rar”,这是一个与多线程相关的压缩文件,很可能包含了用C#或C++编写的相关源代码和文档。通过这个压缩包,我们可以学习到如何在Visual Studio 2005环境下为多普达8125这样的...

    MutiThread.7z

    "MutiThread.7z"这个压缩包很可能包含了与多线程编程相关的代码示例或教程,特别是关于如何在Microsoft Foundation Classes (MFC)框架下实现多线程以及更新进度条的实践案例。 MFC是微软提供的一个C++类库,用于...

    C++MutiThread.pdf

    C++多线程开发是现代C++编程中非常重要的一个部分,它使得程序员能够编写高效执行的并发程序,充分利用多核处理器的计算能力。在C++标准库中,涉及多线程开发主要依赖于、、、、等头文件提供的工具和类。...

    C#多线程实例,超级好懂,附带Word说明文档

    本资源包提供了一个“MutiThread_Test”项目实例,结合了详细的Word文档说明,旨在帮助开发者更好地理解和应用C#中的多线程技术。 在C#中,System.Threading命名空间提供了多线程的支持。Thread类是这个命名空间的...

    VC 多线程编程 C++

    `MutiThread`、`MultiThread3`、`MutiThread2`这些文件名可能代表了不同阶段的多线程示例代码,可能包含创建线程、同步机制的应用、线程间通信等实例。学习这些文件可以帮助理解如何在实际项目中应用多线程技术,...

    java从入门到精通学习笔记-Lesson5(多线程).pdf

    public class MutiThread { public static void main(String[] args) { MyThread mt = new MyThread(); mt.start(); } } ``` 2. **实现 Runnable 接口**:创建一个类实现 Runnable 接口,同样重写 `run()` 方法...

    Delphi多线程实现文件拷贝

    7. **使用MutiThread子文件**:根据提供的压缩包文件名`MutiThread`,我们可以推测这可能是包含实现多线程文件拷贝的源代码文件。实际操作中,我们需要导入并使用这个模块,按照上述步骤创建和管理线程实例,启动...

    windows中多线程编程实例

    在Windows操作系统中,多线程编程是实现并发执行任务的有效方式,它可以充分利用多核处理器的计算能力,提高程序的响应速度和效率。本实例将深入探讨四种不同的同步机制:临界区、事件、信号量和互斥量,这些都是在...

    多线程实现RC4流密码文件加密

    压缩包中的"MutiThread"可能包含实现这些功能的源代码文件,包括主程序、RC4算法实现以及可能的线程管理和同步代码。分析这些源代码可以帮助理解具体实现细节,如线程如何创建、如何交互以及如何测量执行时间。 总...

    定时器quartz的应用

    4. **多线程支持**:Quartz天生支持多线程,可以并发地执行多个任务,这在文件名`mutiThread`中得到了暗示。在高并发场景下,Quartz能确保任务的正确调度和执行,避免了单线程模型下的资源争抢和阻塞问题。 5. **...

    androidMarket:该项目用于我学校的培训课程。这类似于Google市场。有以下一些功能

    2:下载应用程序mutithread。 3:通过排序显示应用4:按主题显示应用5:收集并分享您喜欢的应用6:检查更新。 7:搜索您想要的应用我想添加一些细节,但现在还没有。我的另一个专业人士中还有一个服务器演示。

    linux多线程编程

    在Linux系统中,多线程编程是实现并发执行任务的重要技术。POSIX线程库(pthread)是Linux遵循的一种标准接口,它为开发者提供了创建、管理线程...通过阅读《mutithread.chm》文档,你将更深入地了解这些概念及其实践。

    基于Tcp/Ip的网络聊天室

    主要是使用Tcp/Ip协议来进行网络数据的发送和传播

    winform界面假死状态

    namespace MutiThread { public partial class Form1 : Form { DataTable table; int currentIndex = 0; int max = 10000; public Form1() { InitializeComponent(); } private void buttonOK_Click...

Global site tag (gtag.js) - Google Analytics