Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Wednesday, October 8, 2014
Sunday, September 28, 2014
Tagged under: java, Synchronization, Synchronized, Thread
Sample method
Synchronization
At times when
more than one thread try to access a shared resource, we need to ensure that
resource will be used by only one thread at a time.
The process
by which this is achieved is called synchronization.
Why use Synchronization
To prevent thread interference.
To prevent consistency problem.
Suppose we have
two different threads T1 and T2, T1 starts execution and save certain values in
a file temporary.txt which will be used to calculate some result when T1
returns. Meanwhile, T2 starts and before T1 returns, T2 change the values saved
by T1 in the file temporary.txt (temporary.txt is the shared resource). Now
obviously T1 will return wrong result.
To prevent
such problems, synchronization was introduced. With synchronization in above
case, once T1 starts using temporary.txt file, this file will be locked (LOCK
mode), and no other thread will be able to access or modify it until T1
returns.
Sample method
public class Counter {
private int count = 0;
public synchronized void increment() {
count++;
}
public synchronized int getCount() {
return count;
}
}
- If you declare any method as synchronized, it is known as synchronized method.
- Synchronized method is used to lock an object for any shared resource.
- When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes.
Thursday, September 25, 2014
Tagged under: Autoboxing, java, Unboxing
Autoboxing and Unboxing in Java
Autoboxing is the automatic
conversion that the Java compiler makes between the primitive types and
their corresponding object wrapper classes. If
the conversion goes the other way, this is called unboxing
Autoboxing
Character ch =
'a';
List<Integer>
li = new ArrayList<>();
for (int i = 1; i
< 50; i += 2)
li.add(i); //it creates an Integer object from i and adds the object to li.
******************
List<Integer>
li = new ArrayList<>();
for (int i = 1; i
< 50; i += 2)
li.add(Integer.valueOf(i)); // compiler converts the previous code
to the following at runtime
unboxing
Integer i = new Integer(-8);
// Unboxing
through method invocation
int absVal
= absoluteValue(i);
List<Double> ld = new ArrayList<>();
ld.add(3.1416); // autoboxed through method invocation.
// Unboxing
through assignment
double pi =
ld.get(0);
Primitive type
|
Wrapper class
|
boolean
|
Boolean
|
byte
|
Byte
|
char
|
Character
|
float
|
Float
|
int
|
Integer
|
long
|
Long
|
short
|
Short
|
double
|
Double
|
Benefits
of Autoboxing / Unboxing
- Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
- We don't have to perform explicit typecasting
- It helps prevent errors, but may lead to unexpected results sometimes. Hence must be used with care.
Monday, September 22, 2014
Tagged under: java, Java Development Kit, Java Runtime Environment, Java Virtual Machine, JDK, JRE, JVM
Java
Development Kit
contains tools needed to develop the Java programs, and JRE to
run the programs. The tools include compiler (javac.exe), Java application
launcher (java.exe), Applet viewer, etc…
Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.
You need JDK, if at all you want to write your own programs, and to compile them. For running java programs, JRE is sufficient. JRE is targeted for execution of Java. JDK is mainly targeted for java development.
JVM is divided into several
components like the stack, the garbage-collected heap, the registers and the
method area.
Method Area
Garbage-collected Heap
JDK, JRE and JVM
Java Development Kit (JDK)
Java
Development Kit
contains tools needed to develop the Java programs, and JRE to
run the programs. The tools include compiler (javac.exe), Java application
launcher (java.exe), Applet viewer, etc… Compiler converts java code into byte code. Java application launcher opens a JRE, loads the class, and invokes its main method.
You need JDK, if at all you want to write your own programs, and to compile them. For running java programs, JRE is sufficient. JRE is targeted for execution of Java. JDK is mainly targeted for java development.
Java Runtime Environment (JRE)
Java
Runtime Environment contains JVM, class libraries (like util, math, lang, awt, swing
etc), and other supporting files.
Actually
JVM runs the program, and it uses the class libraries, and other supporting
files provided in JRE. If you want to run any java program, you need to
have JRE installed in the system.
Java Virtual Machine (JVM)
Java Virtual Machine executes the program and generates output. To execute any code, JVM utilizes different components.
The Stack
· Stack in JVM stores various method arguments as
well as the local variables of any method.
Method Area
This
is the area where byte codes reside. The program counter points to some byte in
the method area. It always keeps tracks of the current instruction which is
being executed (interpreted).
Garbage-collected Heap
The
Garbage-collected Heap is where the objects in Java programs are stored.
Whenever we allocate an object using new operator, the heap comes into picture
and memory is allocated from there
Remember
that the local object reference resides on Stack but the actual object resides
in Heap only. Also, arrays in Java are objects, hence they also resides in
Garbage-collected Heap.
Just In Time Compiler (JIT)
JIT is a module inside the JVM which helps in compiling
certain parts of byte code into the machine code for higher performance.
Note that only certain parts of byte code will be compiled to the machine code,
the other parts are usually interpreted and executed.
What is the use of bin and lib in JDK?
- Bin contains all tools such as javac, appletviewer, awt
tool, etc., whereas lib contains API and all packages.
Subscribe to:
Posts (Atom)





