Friday, April 12, 2019

thread setname(),getname(),getid()

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }catch(Exception e){System.out.println(e);} 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 


  System.out.println("Name of t1:"+t1.getName()); 
  System.out.println("Name of t2:"+t2.getName());
  System.out.println("Name of t1:"+t3.getName());
  System.out.println("id of t1:"+t1.getId());
  System.out.println("id of t1:"+t2.getId());
  System.out.println("id of t1:"+t3.getId());
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
   t1.setName("dj thread"); 
   System.out.println("Name of t1:"+t1.getName());
 
 t2.start(); 
 t3.start(); 


 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
Name of t1:Thread-0
Name of t2:Thread-1
Name of t1:Thread-2
id of t1:21
id of t1:22
id of t1:23
1
2
3
Name of t1:dj thread
1
1
2
2
3
3


multithreading with join method


public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<=3;i++){ 
   try{ 
    Thread.sleep(500); 
   }catch(Exception e){System.out.println(e);} 
  System.out.println(i); 
  } 
 } 
public static void main(String args[]){ 
 cl t1=new cl(); 
 cl t2=new cl(); 
 cl t3=new cl(); 
 t1.start(); 
 try{ 
  t1.join(); 
 }catch(Exception e){System.out.println(e);} 
 
 t2.start(); 
 t3.start(); 
 } 
}


$javac cl.java
$java -Xmx128M -Xms16M cl
1
2
3
1
1
2
2
3 
3 

multithreading with start()

public class cl extends Thread{ 
 public void run(){ 
  for(int i=1;i<5;i++){ 
    try{
        Thread.sleep(300);
       
    }
    catch(InterruptedException e)
    {System.out.println(e);} 
    System.out.println(i); 
  } 
 } 
 public static void main(String args[]){ 
  cl t1=new cl(); 
  cl t2=new cl(); 
 
  t1.start(); 
  t2.start(); 
 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
1
1
2
2
3
3
4
4

multithreading with run()

public class cl extends Thread{
 public void run(){
  for(int i=1;i<5;i++){
    try{
        Thread.sleep(300);
     
    }
    catch(InterruptedException e)
    {System.out.println(e);}
    System.out.println(i);
  }
 }
 public static void main(String args[]){
  cl t1=new cl();
  cl t2=new cl();
 
  t1.run();
  t2.run();
 }
}



$javac cl.java
$java -Xmx128M -Xms16M cl
1
2
3
4
1
2
3
4

Java Thread Example by implementing Runnable interface

public class cl implements Runnable{ 
public void run(){ 
System.out.println("thread is running..............................."); 

 
public static void main(String args[]){ 
cl ob=new cl(); 
Thread t1 =new Thread(ob); 
t1.start(); 
 } 




$javac cl.java
$java -Xmx128M -Xms16M cl
thread is running...............................

thread using thread class

code:

public class cl extends Thread{ public void run(){ System.out.println("thread is running.............."); } public static void main(String args[]){ cl ob=new cl(); ob.start(); } }


output:
$javac cl.java
$java -Xmx128M -Xms16M cl
thread is running..............

variable arguments in java.

1. variable-argument-in-java    link
2.  java-program- with overloading -  link
3.  variable-argument- with-overloading   ambiguity - link                                

variable argument with overloading ambiguity in java

// Java program to demonstrate varargs
public class cl
{
// A method that takes variable number of intger
// arguments.


static void f(double x)
{
    System.out.println("overloaded function");
    System.out.println(x);
}

static void f(int x)

{

    System.out.println("overloaded function");

    System.out.println(x);

}

static  void f(int ...a)
{
System.out.println("Number of arguments: " + a.length);


for (int i=0;i<a.length;i++)
System.out.print(i + " ");
System.out.println();
}





public static void main(String args[])
{

f(100); // one parameter
f(1, 2, 3, 4); // four parameters
f();
f(1.1);// no parameter
}
}

$javac cl.java
$java -Xmx128M -Xms16M cl
overloaded function
100
Number of arguments: 4
0 1 2 3 
Number of arguments: 0

overloaded function
1.1

variable argument with overloading in java




// Java program to demonstrate varargs
public class cl
{



static void f(double x)
{
    System.out.println("overloaded function");
    System.out.println(x);
}



static  void f(int ...a)
{
System.out.println("Number of arguments: " + a.length);


for (int i=0;i<a.length;i++)
System.out.print(i + " ");
System.out.println();
}





public static void main(String args[])
{

f(100); // one parameter
f(1, 2, 3, 4); // four parameters
f(); // no parameter
f(1.1);
}
}


output:
$javac cl.java
$java -Xmx128M -Xms16M cl
Number of arguments: 1
0 
Number of arguments: 4
0 1 2 3 
Number of arguments: 0

overloaded function
1.1

variable argument in java

public class cl
{
// A method that takes variable number of intger
// arguments.
static  void f(int ...a)
{
System.out.println("Number of arguments: " + a.length);


for (int i=0;i<a.length;i++)
System.out.print(i + " ");
System.out.println();
}

// Driver code
public static void main(String args[])
{
// Calling the varargs method with different number
// of parameters
f(100); // one parameter
f(1, 2, 3, 4); // four parameters
f(); // no parameter
}
}


output:
$javac cl.java
$java -Xmx128M -Xms16M cl
Number of arguments: 1
0 
Number of arguments: 4
0 1 2 3 
Number of arguments: 0