Posts

Showing posts from August, 2014

Thread priority in java with example

Thread priority is useful to maintain threads in an order in java. When multiple threads are created and started, a 'Thread Scheduler' program will load all threads into JVM memory and executes them based on thread priority. This scheduler will allot more JVM time to those threads which are having higher priorities.           The priority number of a thread will change from 1 to 10. The minimum priority ( Thread.MIN_PRIORITY ) of a thread is 1, and the maximum priority ( Thread.MAX_PRIORITY ) is 10. The normal priority of a thread ( Thread.NORM_PRIORITY ) is 5. Example: Let us write a program to understand the thread priority. The thread with higher priority number will complete its execution first. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package com . javatbrains . threads ; class ThreadPriority extends Thread { int count = 0 ; public void run () { for (

Multithreading in java with example

Multithreading  is one of the most important concept in core java. In this article we will learn what is multithreading? , what is the use of it? and What is the use of Synchronization and when to use it?  with detailed examples. At a time, two or more threads are accessing the same object is called as Multithreading  in Java .  First, we will create two threads for two objects. It is also possible to run two or more threads on a single class object. In this case, there is a possibility to get unreliable results. If the two threads are perform same task, then they need same object to be executed each time. For your better understanding, take an example of any reservations like, railway, movie ticket booking,etc. Let us think only one berth is available in a train and two passengers are asking for that berth. The first person has sent a request to allocate that ticket/berth to him. At the same time, the second person also sent a request to allocate that ticket/berth to him

how to create our own thread in java with example

To create user defined thread in java, create a class that extends Thread or implements Runnable interface. Both of the Thread class and Runnable interface are available in java.lang package. class MyClass extends Thread OR class MyClass implements Runnable         •   Now in this class, write a run() method as, public void run (){ statements ; }        by default, this run() method is recognised and executed by a thread when you started a thread.        •    Create an object of MyClass, So that the run() method is available for execution. MyClass obj = new MyClass ();       •    Now, Create a thread and attach the thread to the object obj. Thread t = new Thread ( obj );       •    Run the thread. For this purpose, we should use start() method of Thread class. t . start ();          Now, the thread will start execution on the object of MyClass. In that object, run() method is found, henc

Threads in java with example

Image
Read this article to know what is thread and what is the lifecycle of thread in java. Threads concept introduced in java to achieve multiprocessing. Thread represents a separate path of program execution. All threads are running on top of the Main thread in java. This main thread is called daemon thread. Explanation: A thread represents a separate path of execution of a group of statements. In a Java program, if we write a group of statements, then these statements are executed by JVM one by one. This execution is called thread, because JVM uses a thread to execute these statements. This means in every Java program there is thread is running internally. Let us see the below program to find the thread used by JVM to execute the statements: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com . javatbrains . threads ; public class CurrentThread { public static void main ( String [] args ) { System . out . println ( "Let us f