6. Check Oddity
Have a look at the lines of code below and
determine if they can be used to precisely identify if a given number is Odd?
public boolean oddOrNot(int num) {
return num % 2 ==
1;
}
These lines seem correct but they will return
incorrect results one of every four times (Statistically speaking). Consider a
negative Odd number, the remainder of division with 2 will not be 1. So, the
returned result will be false which is incorrect!
This can be fixed as follows:
public boolean oddOrNot(int num) {
return (num & 1) != 0;
}
Using this code, not only is the problem of
negative odd numbers solved, but this code is also highly optimized. Since,
Arithmetic and Logical operations are much faster compared to division and
multiplication, the results are achieved faster so in second snippet.
7. Difference between single quotes and double
quotes
public class Haha {
public static void main(String args[]) {
System.out.print("H" + "a");
System.out.print('H' + 'a');
}
}
From the code, it would seem return “HaHa” is
returned, but it actually returns Ha169. The reason is that if double quotes
are used, the characters are treated as a string but in case of single quotes,
the char -valued operands ( ‘H’ and ‘a’ ) to int values through a process known
as widening primitive conversion. After integer conversion, the numbers are
added and return 169.
8. Avoiding Memory leaks by simple tricks
Memory leaks often cause performance degradation of
software. Since, Java manages memory automatically, the developers do not have
much control. But there are still some standard practices which can be used to
protect from memory leakages.
• Always
release database connections when querying is complete.
• Try
to use finally block as often possible.
• Release
instances stored in Static Tables.
9. Avoiding Deadlocks in Java
Deadlocks can occur for many different reasons.
There is no single recipe to avoid deadlocks. Normally deadlocks occur when one
synchronized object is waiting for lock on resources locked by another
synchronized object.
Try running the below program. This program
demonstrates a Deadlock. This deadlock arises because both the threads are waiting
for the resources which are grabbed by other thread. They both keep waiting and
no one releases.
01 public class
DeadlockDemo {
02 public static Object addLock = new Object();
03 public static Object subLock = new Object();
04
05 public static void main(String args[]) {
06
07 MyAdditionThread add = new
MyAdditionThread();
08 MySubtractionThread sub = new
MySubtractionThread();
09 add.start();
10 sub.start();
11 }
12 private static class
MyAdditionThread extends Thread {
13 public void run() {
14 synchronized (addLock) {
15 int a = 10, b = 3;
16 int c = a + b;
17 System.out.println("Addition
Thread: " + c);
18 System.out.println("Holding
First Lock...");
19 try { Thread.sleep(10); }
20 catch (InterruptedException e) {}
21 System.out.println("Addition
Thread: Waiting for AddLock...");
22 synchronized (subLock) {
23
System.out.println("Threads: Holding Add and Sub Locks...");
24 }
25 }
26 }
27 }
28 private static class MySubtractionThread
extends Thread {
29 public void run() {
30 synchronized (subLock) {
31 int a = 10, b = 3;
32 int c = a - b;
33
System.out.println("Subtraction Thread: " + c);
34 System.out.println("Holding
Second Lock...");
35 try { Thread.sleep(10); }
36 catch (InterruptedException e) {}
37
System.out.println("Subtraction
Thread: Waiting for SubLock...");
38 synchronized (addLock) {
39
System.out.println("Threads: Holding Add and Sub Locks...");
40 }
41 }
42 }
43 }
44 }
Output:
1 =====
2 Addition Thread: 13
3 Subtraction Thread: 7
4 Holding First Lock...
5 Holding Second Lock...
6 Addition Thread: Waiting
for AddLock...
7 Subtraction Thread: Waiting for SubLock...
But if the order in which the threads are called is
changed, the deadlock problem is resolved.
01 public class
DeadlockSolutionDemo {
02 public static Object addLock = new Object();
03 public static Object subLock = new Object();
04
05 public static void main(String args[]) {
06
07 MyAdditionThread add = new
MyAdditionThread();
08 MySubtractionThread sub = new
MySubtractionThread();
09 add.start();
10 sub.start();
11 }
12
13
14 private static class
MyAdditionThread extends Thread {
15 public void run() {
16 synchronized (addLock) {
17 int a = 10, b = 3;
18 int c = a + b;
19 System.out.println("Addition
Thread: " + c);
20 System.out.println("Holding
First Lock...");
21 try { Thread.sleep(10); }
22 catch (InterruptedException e) {}
23 System.out.println("Addition
Thread: Waiting for AddLock...");
24 synchronized (subLock) {
25 System.out.println("Threads:
Holding Add and Sub Locks...");
26 }
27 }
28 }
29 }
30
31 private static class MySubtractionThread
extends Thread {
32 public void run() {
33 synchronized (addLock) {
34 int a = 10, b = 3;
35 int c = a - b;
36
System.out.println("Subtraction Thread: " + c);
37 System.out.println("Holding
Second Lock...");
38 try { Thread.sleep(10); }
39 catch (InterruptedException e) {}
40 System.out.println("Subtraction Thread: Waiting for SubLock...");
41 synchronized (subLock) {
42
System.out.println("Threads: Holding Add and Sub Locks...");
43 }
44 }
45 }
46 }
47 }
Output:
1 =====
2 Addition Thread: 13
3 Holding First Lock...
4 Addition Thread: Waiting
for AddLock...
5 Threads: Holding Add and
Sub Locks...
6 Subtraction Thread: 7
7 Holding Second Lock...
8 Subtraction Thread: Waiting for SubLock...
9 Threads: Holding Add and
Sub Locks...
10. Reserve memory for Java
Some of the Java applications can be highly CPU
intensive as well as they need a lot of RAM. Such applications generally run
slow because of a high RAM requirement. In order to improve performance of such
applications, RAM is reserved for Java. So, for example, if we have a Tomcat
webserver and it has 10 GB of RAM. If we like, we can allocate RAM for Java on
this machine using the following command:
1 export
JAVA_OPTS="$JAVA_OPTS -Xms5000m -Xmx6000m -XX:PermSize=1024m
-XX:MaxPermSize=2048m"
• Xms
= Minimum memory allocation pool
• Xmx
= Maximum memory allocation pool
• XX:PermSize
= Initial size that will be allocated during startup of the JVM
• XX:MaxPermSize
= Maximum size that can be allocated during startup of the JVM
No comments:
Post a Comment