Wednesday, November 5, 2014

JAVA USING PROGRAM



Get Current Thread
public class GetCurrentThreadExample
{
 public static void main(String[] args)
{
Thread currentThread = Thread.currentThread();
System.out.println(currentThread);
   }
}
Pause Thread Using Sleep Method
public class PauseThreadUsingSleep
{
public static void main(String[] args)
{
System.out.println("Print number after pausing for 1000 milliseconds");
try
{
 for(int i=0; i< 5; i++)
{
 System.out.println(i);
 Thread.sleep(1000);
   }
}
catch(InterruptedException ie)
{
System.out.println("Thread interrupted !" + ie);
                           }
    }
}
Set Thread Name
public class SetThreadNameExample
{
public static void main(String[] args)
{
Thread currentThread = Thread.currentThread();
System.out.println(currentThread);
currentThread.setName("Set Thread Name Example");
System.out.println("Thread Name : "+ currentThread.getName());
   }
}
Java continue statement
public class JavaContinueExample
{
public static void main(String[] args)
{
int intArray[] = new int[]{1,2,3,4,5};
System.out.println("All numbers except for 3 are :");
for(int i=0; i < intArray.length; i++)
{
if(intArray[i] == 3)
continue;
else
System.out.println(intArray[i]);
    }
  }
}


Java continue statement with label
public class JavaContinueWithLabelExample
{
public static void main(String[] args)
{
int intArray[][] = new int[][]{{1,2},{2,3}};
for(int i=0; i < intArray.length; i++)
{
for(int j=0; j < intArray[i].length ; j++)
{
if(intArray[i][j] == 3)
continue Outer;
System.out.println("Element is : " + intArray[i][j]);
          }
        }
     }
}
Java break statement
public class JavaBreakExample
 {
public static void main(String[] args)
{
int intArray[] = new int[]{1,2,3,4,5};   
System.out.println("Elements less than 3 are : ");
for(int i=0; i < intArray.length ; i ++)
{  
if(intArray[i] == 3)
break;
else
System.out.println(intArray[i]);
     }
   }
}




No comments:

Post a Comment