Home Core Java Overview Maven Tutorials

Thursday, 11 April 2019

MultiThreading in java

MultiThreading in java

Multitasking and Multithreading

1). Multitasking refers to a computer's ability to perform multiple jobs concurrently. For Example :- more than one program are running concurrently.
2).A Thread is a single sequence of execution within a program.
MultiThreading in java is a process of executing multiple threads simultaneously.
Multithreading refers to multiple threads of control with in a single program.
Each program can run multiple threads of control within it.

A Thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading both are used to acheive multitasking.
Threads use a shared memory area but processes are not.
A thread is a lightweight subprocess, the smallest unit of processing. It is a separate path of execution.
Threads are independent. If there occurs exception in one thread, it doesn't affect other threads. It uses a shared memory area.
Java Multithreading is mostly used in games, animation etc.

For Example : Web browser is a program and its tabs are single thread.

Process :- An execution instance of a program is called a process. A thread is a subset of the process.

Advantage of Thread

1). To maintain responsiveness of an application during a long running task
2).To enable cancellation of separable tasks.
3).Some problems are intrinsically parallel.
4).To monitor status of some resource (DB)
5). Some APIs and Systems demand it : Swing.

Advantages of Java Multithreading

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.

Application Thread

When we execute an application :-
The JVM creates a Thread object whose task is defined by the main() method.
It Starts the thread.
The thread executes the statements of the program one by one until the method returns and the thread dies.

Multiple Threads in an Application

Each thread has its private run-time stack
if two threads execute the same method, each will have its own copy of the local variables the methods uses.
However, all the threads see the same dynamic memory(heap)
Two different threads can act on the same object and same static fields concurrently.

Creating Thread

There are two ways to create our own Thread Object
1). By Extending Thread class OR Subclassing the Thread class and instantiating a new object of that class.
2). Implementing the Runnable interface.
In both the case the run() method should be implemented.

Dictionary and serialization in Java

Dictionary and serialization in Java

Dictionary:- Dictionary is an obstract class that represents a key/value storage repository and operates much like Map.
With dictionaries, we can
2). FIND
public class DisctionaryPractice
public static void main(Striing[] args)
Map stardictionary = new HashMap();
stardictionary.put("Julia", "America");
stardictionary.put("Nicole", "USA");
stardictionary.put("Kiera", "UK");
stardictionary.put("Anne", "new york");

Serialization in Java

Serialization in java is a mechanism of writing the state of an object into a byte stream.
In other Word
Serialization is a mechanism of convertion the state of an object into a byte stream.
The reverse operation of serialization is called deserialization.
In other Word
Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory.
The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.
To make a Java object serializable we implement the interface.
The ObjectOutputStream class contains writeObject() method for serializing an Object.

Advantage of Java Serialization

It is mainly used to travel object's state on the network(known as marshaling).

Example of Serialization:-

class Employee implements Serializable
int empid;
String empname;
public Employee(int empid, String empname)
this.empid = empid;
this.empname = empname;
public class Persist
public static void main(String[] args)
Employee emp = new Employee(308, "Chandan");
FileOutputStream fout = new FileOutputStream("file.text");
ObjectOutputStream Oout = new ObjectOutputStream(fout);
System.out.println("Serialization Completed");

Example of Deserialization:-

class Employee implements Serializable
int empid;
String empname;
public Employee(int empid, String empname)
this.empid = empid;
this.empname = empname;
public class Depersist
public static void main(String[] args) throws Exception
FileInputStream fin = new FileInputStream("file.txt");
ObjectInputStream Oin = new ObjectInputStream(fin);
Employee emp = (Employee)Oin.readObject();
System.out.println(emp.empid+ " " + emp.empname);
System.out.println("DeSerialization Completed");

Monday, 8 April 2019

Data Structure in JAVA

Data Structure in Java, what is Enumeration, vector etc.?

The Enumeration

:- It is an Interface
It is available in Java utility package.
Enumeration is used to process the elements of the collection objects which are introduce in jdk 1.0 for example vector.

There are two types of methods declared by Enumeration :-
hasMoreElements :- It returns Boolean value like True or False
nextElement :- It returns the next object in the enumeration.

Here is the Example for Vector & Enumeration :-

import java.util.Vector;
import java.util.Enumeration;

public class ENUM
public static void main(String args[])
Enumeration name;
Vector starNames = new Vector(5,3); (here 5 & 3 are initial & incremental capacity)
starNames.add("Julia Robert");
starNames.add("keanu reeves");
name = starNames.elements(); //It will return Enumeration object
while (name.hasMoreElements())

Output :-

Julia Robert
keanu reeves

Vector :- It is a Class
Vector implements a dynamic array.
Implements List Interface & extends AbstractList.
Vector v = new Vector() Creates default vector of capacity 10.
Vector v = new Vector(int size, int incre) defines vectors Initial size & incremental size.

Example of vector class :-

public class Vectortutorial
Enumeration enum;
public static void main(String[] args)
Vector v = new Vector();
v.add("Testing Tutorials");
Iterator itr = v.iterator();
Vector vec = new Vector();
enum = vec.elements();
while (enum.hasMoreElements())

Output :-

Testing Tutorials

Difference between Vector & ArrayList In Java

1). java.util.Vector came along with the first version of java development kit(JDK).
java.util.ArrayList was introduced in java version 1.2 as part of Java collection framework.
2). Vector is synchronized but ArrayList is not or we can say also
"All the methods of vector is synchronized but the methods of ArrayList is not synchronized."
So ArrayList is fast because it is not synchronised & vector is slow because it is synchronized.
3). Vectors doubles(100%) the size of its when its size is increased and ArrayList increases by half(50%) of its size when its size is increased.
4). ArrayList uses Iterator Interface to traverse the elements but A vector can use Iterator interface or Enumeration interface to traverse the elements.