Multithreading
- 07 June
Daemon thread in java with example
Daemon threads are low priority background threads which provide services to user threads. It’s life depends on user threads. If no user thread is running then JVM can exit even if daemon threads are running. JVM do not wait for daemon threads to finish. Daemon threads perform background tasks such as garbage collection, finalizer etc. […]
- 17 May
Can we call run() method directly to start a new thread
No, you can not directly call run method to start a thread. You need to call start method to create a new thread. If you call run method directly , it won’t create a new thread and it will be in same stack as main. Lets understand with the help of example: [crayon-676802ac9d4d5151538033/] When you […]
- 16 May
Can we start a thread twice in java
No, Once you have started a thread, it can not be started again. If you try to start thread again , it will throw IllegalThreadStateException Lets understand with the help of example: [crayon-676802ac9d58e151173289/] When you run above program , you will get below output: Thread is runningException in thread “main” java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:705) at org.arpit.java2blog.StartThreadAgainMain.main(StartThreadAgainMain.java:16) […]
- 05 December
BlockingQueue in java
BlockingQueue is introduced in java with concurrent package with ConcurrentHashMap. It is thread safe queue to put and take elements from it. BlockingQueue is special type of queue which is used when one thread produces object and another thread consumes it. Producer thread will keep inserting objects to queue until it reaches upper limit. Once […]
- 08 August
CountDownLatch in java
As per java docs, CountDownLatch is synchronisation aid that allows one or more threads to wait until set of operations being performed in other threads completes.
- 10 July
wait, notify and notifyAll method in java with example
You might have noticed Object class has three final method called wait, notify and notifyAll. These methods are used for inter thread communication. Â Java 5 has introduced executor framework which takes care of inter thread communication for you and internally uses wait, notify and notifyAll but you still require basic understanding of these methods and […]
- 16 June
Java Thread Join Example
In this post, we will see about Java thread join method. Thread class’s join() method can be used to stop current execution of thread until thread it joins, completes its task. So basically, it waits for the thread on which join method is getting called, to die.For example: Here when you call t1.join(), main thread […]
- 14 June
Java Thread Sleep
Sleep method of java.lang.Thread is used to pause current execution of thread for specific period of time. Some important points about sleep method are : It causes current executing thread to sleep for specific amount of time. Its accuracy depends on system timers and schedulers. It keeps the monitors it has acquired, so if it […]
- 14 June
Java Thread example
Thread can be called as light weight process. Java supports multithreading , so it allows your application to perform two or more task concurrently. Â Multithreading can be of advantage specially when now a days, machine has multiple CPUs, so multiple tasks can be executed concurrently. Whenever we call main method in java, it actually creates […]