Posts

Showing posts from August, 2019

create thread in multi threading

How to create thread There are two ways to create a thread: By extending Thread class By implementing Runnable interface. Thread class: Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface. Runnable interface: The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run(). public void run():  is used to perform action for a thread. Starting a thread: start() method  of Thread class is used to start a newly created thread. It performs following tasks: A new thread starts(with new callstack). The thread moves from New state to the Runnable state. When the thread gets a chance to execute, its target run() method will run. 1) Java Thread Example by extending Thread class class  Multi  extends  Thread{   public ...

Life cycle of a Thread

Image
Life cycle of a Thread A thread can be in one of the five states. According to sun, there is only 4 states in  thread life cycle in java  new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread in java is controlled by JVM. The java thread states are as follows: New Runnable Running Non-Runnable (Blocked) Terminated Following are the stages of the life cycle − New  − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a  born thread . Runnable  − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. Waiting  − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A threa...

Java Thread Methods

Java Thread Methods S.N. Modifier and Type Method Description 1) void start() It is used to start the execution of the thread. 2) void run() It is used to do an action for a thread. 3) static void sleep() It sleeps a thread for the specified amount of time. 4) static Thread currentThread() It returns a reference to the currently executing thread object. 5) void join() It waits for a thread to die. 6) int getPriority() It returns the priority of the thread. 7) void setPriority() It changes the priority of the thread. 8) String getName() It returns the name of the thread. 9) void setName() It changes the name of the thread. 10) long getId() It returns the id of the thread. 11) boolean isAlive() It tests if the thread is alive.

Multithreading in Java

Multi threading in Java A thread is a lightweight sub-process, the smallest unit of processing.  Multiprocessing and multi threading, both are used to achieve multitasking. Multi threading  in java  is a process of executing multiple threads simultaneously. Advantages of Java Multi threading : 1) It  doesn't block the user  because threads are independent and you can perform multiple operations at the same time. 2) You  can perform many operations together, so it saves time . 3) Threads are  independent , so it doesn't affect other threads if an exception occurs in a single thread.

StringBuffer class Example Program

Java StringBuffer class Java StringBuffer class is used to create mutable (modifiable) string. The StringBuffer class in java is same as String class except it is mutable i.e. it can be changed. Example Program : class StringBufferDemo { public static void main(String args[]) { StringBuffer str=new StringBuffer("WELCOME"); System.out.println(str); System.out.println(str.append(" to JAVA")); System.out.println(str.insert(4,"HOW")); System.out.println(str.delete(4,7)); System.out.println(str.reverse()); System.out.println(str.indexOf("WELCOME")); System.out.println(str.charAt(6)); System.out.println(str.substring(8));  } }

String in java

What is String in java Generally, String is a sequence of characters. But in Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object. How to create a string object? There are two ways to create String object: By string literal By new keyword String Literal Java String literal is created by using double quotes.  For Example:  String s= "welcome" ;    By new keyword String s= new  String( "Welcome" ); //creates two objects and one reference variable Example Program: class StringDemo { public static void main(String args[]) { String str=new String("Hello"); System.out.println(str); System.out.println(str.length()); System.out.println(str.indexOf("H")); System.out.println("Character at="+str.charAt(4)); System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); Syste...

Vector Example program

Java Vector Class Java Vector class comes under the java.util package. The vector class implements a growable array of objects. Like an array, it contains the component that can be accessed using an integer index. Vector is very useful if we don't know the size of an array in advance or we need one that can change the size over the lifetime of a program. Vector implements a dynamic array that means it can grow or shrink as required. It is similar to the ArrayList, but with two differences - Vector is synchronized. The vector contains many legacy methods that are not the part of a collections framework Three ways to create vector class object: 1.Vector vec=new Vector() Method 2: Syntax:  Vector object= new Vector(int initialCapacity) Method 3: Vector object = new vector ( int initialcapacity , capacityIncrement ) Example program : import  java.util.*;   public   class  VectorExample1 {   ...

One Dimensional Array

One Dimensional Array – Using Standard Method class OnedimensionalStandard { public static void main ( String args [ ] ) {      int [ ] a = new int [ 3 ] ; //declaration   a [ 0 ] = 10 ; //initialization   a [ 1 ] = 20 ;    a [ 2 ] = 30 ;    //printing array   System . out . println ( "One dimensional array elements are" ) ;      System . out . println ( a [ 0 ] ) ;      System . out . println ( a [ 1 ] ) ;      System . out . println ( a [ 2 ] ) ;      } } Using Scanner Read the array length as sc.nextInt() and store it in the variable len and declare an array int[len]. 2)  To store elements in to the array for i=0 to i<length of an array read the element using sc.nextInt() and store the element at the index a[i]. 3)  Display the elements of an array f...