Translate

Wednesday, 8 January 2014

Create multiple threads in java and assign different sleep() time to each Thread.

//Test.java

class simpleThread extends Thread
{
public simpleThread(String str)
{
super(str);
}
public void run()
{
for(int i=0;i<5;i++)
{
try{
String threadName=getName();
if(threadName.equals("A")){
System.out.println(i +": Thread Name: " + getName());
sleep(1000);
}
else if(threadName.equals("B")){
System.out.println("                    "+i +": Thread Name: " + getName());
sleep(3000);
}
else
{
System.out.println("                                        "+i +": Thread Name: "+ getName());
sleep(5000);
}
}
catch(Exception e){System.out.println(e);}
}
}
}

class Test
{
public static void main(String abc[])
{
System.out.println("-----------------------------------------------------------");
new simpleThread("A").start();
new simpleThread("B").start();
new simpleThread("C").start();
}
}

Simple example of Multi Threading in java. Create multiple threads in java.

//Test.java

class simpleThread extends Thread
{
public simpleThread(String str)
{
super(str);
}
public void run()
{
for(int i=0;i<5;i++)
System.out.println(i +": Thread Name: " + getName());
{
try{
sleep(1000);
}
catch(Exception e){System.out.println(e);}
}
}
}

class Test
{
public static void main(String abc[])
{
System.out.println("-----------------------------------------------------------");
new simpleThread("A").start();
new simpleThread("B").start();
}
}

Monday, 30 December 2013

Static Initialization Blocks with a simple example

A static initialization block is a normal block of code enclosed 
in braces, { }, and preceded by the static keyword. Here is an example: 

static { 
 // whatever code is needed for initialization goes here
} 

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. 
The run-time system guarantees that static initialization blocks are called in the order that they appear in the source code.
For example,
Consider following code.

class StaticBlockDemo 
{ 
static 
{ 
System.out.println("I am in static block 1"); 
} 
static 
{ 
System.out.println("I am in static block 2"); 
} 
public static void main(String args[]) 
{ 
System.out.println("I am in main() method"); 
} 
}

Output:
 
I am in static block 1
I am in static block 2
I am in main() method

So you can see that static block initialization takes place before main() method. 
Static blocks are executed sequentially. Here we have two static blocks. 
Block 1 executed prior to Block 2. 
If we change order of blocks to something like 

static 
{ 
System.out.println("I am in static block 2"); 
} 

static 
{ 
System.out.println("I am in static block 1"); 
} 

 
then output will be

I am in static block 2
I am in static block 1

 
Use of static block:

Generally, static block is used to perform initialization.

Tuesday, 24 December 2013

Write a java code to print goat

class goat
{
public static void main(String abc[])
{
System.out.println();
System.out.println();
System.out.println("               <___>");
System.out.println("               (o-o) ");
System.out.println("   +============\\_/  ");
System.out.println("  / || %%%%%  ||      ");
System.out.println(" /  || %%%%%  ||      ");
System.out.println("*   ||--------||      ");
System.out.println("    \"\"        \"\"      ");
}
}

Note: Just copy paste the above code in notepad file and run it. I hope you will enjoy this. Also, from this code you will learn about the importance of escape characters in a program.

Sunday, 29 September 2013

Write a java code to get an ascii value of a given character

Solution:

import java.io.*;
class CharToAscii
{
public static void main(String abc[])
{
System.out.println("Enter a character");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
int c=br.read();
System.out.println("The ascii value of above character is " + c);
}
catch(Exception e)
{
System.out.println("Error...");
}
}}

Output:
C:\> java CharToAscii
Enter a character
A
The ascii value of above character is 65

Write a java code to sort an array

Solution:

class Sort
{
public static void main(String abc[])
{
int temp;
int arr[]={3,7,8,5,2,1,9,6,4};
for(int i=0;i<arr.length;i++)
{
for(int j=i+1;j<arr.length;j++)
{

//Use arr[i]<arr[j] if you want to sort an array in descending order.
//Note: The below condition will sort an array in ascending order.

if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}

}

Write a java code to print square

*********
*           *
*           *
*           *
*    *     *
*           *
*           *
*           *
*********

Solution:-

class square
{
public static void main(String abc[])
{
int size=9;                 //You can change the value of size variable to minimize or maximize square
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(i>=1&&i<=size-2&&j>=1&&j<=size-2)
{
if(i==size/2&&j==size/2)
{
System.out.print("*");
}
else{
System.out.print(" ");
}
}
else
{
System.out.print("*");
}

}
System.out.println();
}
}

}