Sunday, June 19, 2016

Thread Programs

//import java.lang.String;

class odd extends Thread{

odd(String str){
super(str);
}

public void run(){
System.out.println("THREAD NAME: "+getName());
try{
for (int i=1;i<=50;i++){
if(i%2!=0)
System.out.println("odd thread: "+ i );
Thread.sleep(500);
}
}
catch(InterruptedException e){
System.out.println(e);
}
}
}

class even extends Thread{
even(String str){
super(str);
}
public void run()
{
System.out.println("THREAD NAME: "+getName());
try{
for (int i=1;i<=50;i++){
if(i%2==0)
System.out.println("even thread: "+ i);
Thread.sleep(500);
}
}
catch(InterruptedException o){
System.out.println(o);
}
}
}



class tdisplay{
public static void main(String args[]){
System.out.println("display numbers");
odd o= new odd("odd");
even e= new even("even");
o.start();
e.start();
System.out.println(e);
}
}public class threads{
public static void main(String args[]){

try{
System.out.println("thread program");

Thread t= new Thread();


for (int i=0;i<10 i="" p="">{

System.out.println(i+1);
t.sleep(5000);

}

}
catch(InterruptedException  e)
{
System.out.println("error is : " + e);

}

}
}class odd extends Thread{

public void run(){
try{

for (int i=1;i<=50;i++){

if(i%2!=0)
System.out.println("odd thread: "+ i );
//Thread.sleep(500);

}
}
//catch(InterruptedException e){
//System.out.println(e);
//}


finally{
}
}

}

class even extends Thread{

public void run()
{

try{
for (int i=1;i<=50;i++){
if(i%2==0)
System.out.println("even thread: "+ i);
//Thread.sleep(500);
}
}

//catch(InterruptedException o){
//System.out.println(o);
//}

finally{
}
}

}



class tpriority{

public static void main(String args[]){

System.out.println("display numbers");

odd o= new odd();
even e= new even();
e.setPriority(1);
o.setPriority(10);
o.start();
e.start();

}
}class one extends Thread {

public void run(){
try{
for(int i=0;i<3 i="" p="">{
sleep(1000);
System.out.println ("good morning");
}
}
catch(InterruptedException e){
}


}

}
class two extends Thread{

public void run(){
try{
for(int i=0;i<3 i="" p="">sleep(2000);
System.out.println("hello");
}
}
catch(InterruptedException e){

}

}

}
class three extends Thread{

public void run(){
try{
for(int i=0;i<3 i="" p="">sleep(3000);
System.out.println("welcome");

}
}
catch(InterruptedException e){

}


}

}
public class ttimer{
public static void main(String args[])
{
System.out.println("thread timer program");
one o= new one();
two tw = new two();
three th= new three();

o.start();
tw.start();
th.start();

}

}class one extends Thread {

public void run(){
try{
sleep(1000);
System.out.println ("good morning");

}
catch(InterruptedException e){
}


}

}
class two extends Thread{

public void run(){
try{
sleep(2000);
System.out.println("hello");

}
catch(InterruptedException e){

}

}

}
class three extends Thread{

public void run(){
try{
sleep(3000);
System.out.println("welcome");


}
catch(InterruptedException e){

}


}

}
public class ttimer1{
public static void main(String args[])
{
System.out.println("thread timer program");
one o= new one();
two tw = new two();
three th= new three();

o.start();
tw.start();
th.start();

}

}

No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...