Nov 24

How to set thread name in java?

[code lang=”java”]public class ThreadNameDemo extends Thread {

public void run() {

System.out.println("Iam in run method " + getName());

}

public static void main(String[] args) {

ThreadNameDemo t1 = new ThreadNameDemo();

//setting the name First Thread to the running thread.
t1.setName("First Thread");

//Thread execution gets started by calling run method
t1.start();

}

}[/code]

Output :
Iam in run method First Thread

[pullquote]we can give the thread name using setName method in Thread class Which accepts String arguments[/pullquote]

Nov 24

Thread sleep method example in java

[code lang=”java”]
public class SleepDemo extends Thread{

public void run()
{
for(int i=0;i<10;i++)
{

System.out.println("Iam in run method"+i);
try {

// causes thread to sleep for specified amount of time.
Thread.sleep(1000);

} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
public static void main(String[] args) {

SleepDemo t =new SleepDemo();

//start method internally calls run method
t.start();
}

}
[/code]

Output :

Iam in run method0
Iam in run method1
Iam in run method2
Iam in run method3
Iam in run method4
Iam in run method5
Iam in run method6
Iam in run method7
Iam in run method8
Iam in run method9

[pullquote]sleep method is a staic method and throws checked exception
Interrupted Exception.sleep() method causes the running thread to
sleep for specified amount of time.When the time expires thread
automatically becomes active.
[/pullquote]

Nov 24

Creating threads by implementing Runnable Interface Example

[code lang=”java”]public class RunnableDemo implementsĀ  Runnable{

public void run() {
System.out.println("Iam in run method");
}

public static void main(String[] args) {
RunnableDemo runnableDemo=new RunnableDemo();
//creates new thread but execution is not started.
Thread t= new Thread(runnableDemo);
// start internally calls run method
t.start();

}

}[/code]

[pullquote]Runnable interface is one of the way to create threads. whenver we implement runnable interface we need to implement run method.If we want to just implement run method without using any thread class method it is better to implement runnable interface rather than Thread class.[/pullquote]

Nov 23

Creating Threads in java by Extending Thread class

[code lang=”java”]public class ThreadDemo extends Thread {

public void run() {
System.out.println("Iam in run method");
}

public static void main(String[] args) {

// creating thread instance
ThreadDemo t = new ThreadDemo();

//Thread execution begins with start method .
//start() method internally calls run method.

t.start();
}

}

[/code]


[pullquote]Thread starts execution only when start method is called,
till that time thread will be in new state.start() method internally calls run method
[/pullquote]

Output :
Iam in run method

Nov 06

HCL hiring freshers from Nov7th 2013 to Nov 9th 2013

HCL hiring technical support officers starting from Nov7th to Nov9th 2013.

JobĀ  Posted Date Nov 9th 2013
Company` HCL Technologies
Experience Freshers
Designation Technical Support Officers
Industry IT-Software/ Software Services
Location chennai
Education Any Degree,PG
Technical Skills
Official website http://hcl.com/
Contact click here for more details and how to apply
Walk-in Nov 9th 2013
Nov 04

SONAR-Avoid using implementation types like HashMap use interface instead

SONAR-Loose coupling

We get this violation when we use Hash implementation as follows.It is bad practice to use Map implementation as given below.

public class LooseCouplingExample {

HashMap<String,Integer> hashMap=new HashMap<String,Integer>();

}

To clear sonar violation use the above statement as given below.

public class LooseCouplingExample {

Map<String,Integer> hashMap=new HashMap<String,Integer>();

}

It is recommended to use as shown above because there is no tight coupling with hashmap. In future if we want to change the Hashmap implementation to some other map implementation we can do so easily without many changes.