Java Thread by Extending Thread Class
package com.myjava.threads;
class mysmpthread extends thread
{
public static int mycount
= 0;
public void run()
{
while(mysmpthread.mycount
<= 10)
{
try
{
system.out.println("Expl
Thread: "+(++mysmpthread.mycount));
thread.sleep(100);
}
catch (interruptedexception iex)
{
System.out.println("Exception
in thread: "+iex.getmessage());
}
}
}
}
public class runthread
{
public static void
main(string a[])
{
system.out.println("Starting
Main Thread...");
mysmpthread
mst = new mysmpthread();
mst.start();
while(mysmpthread.mycount
<= 10)
{
try
{
system.out.println("Main
Thread: "+(++mysmpthread.mycount));
thread.sleep(100);
}
catch (interruptedexception iex)
{
system.out.println("Exception
in main thread: "+iex.getmessage());
}
}
system.out.println("End
of Main Thread...");
}
}
Java Daemon Thread Examples
package com.myjava.threads;
public class daemonthread extends thread
{
public daemonthread()
{
setdaemon(true);
}
public void run()
{
system.out.println("Is
this thread Daemon? - "+isdaemon());
}
public static void
main(string a[])
{
daemonthread
dt = new daemonthread();
dt.start();
}
}
Java Thread Join Examples
package com.myjava.threads;
import java.util.arraylist;
import java.util.list;
public class mythreadjoin
{
public static list
names = new arraylist();
public static void main(string a[])
{
list
list = new arraylist();
for(int
i=0;i<5 i="" span="">5>
{
samplethread
s = new samplethread();
list.add(s);
s.start();
}
for(samplethread
st:list)
{
try
{
st.join();
}
catch (exception ex){}
}
system.out.println(names);
}
}
class samplethread extends thread
{
public void run()
{
for(int
i=0; i<10 i="" span="">10>
{
try
{
thread.sleep(10);
}
catch(exception ex){}
}
mythreadjoin.names.add(getname());
}
}
Java Thread Sleep
package com.myjava.threads;
import java.util.arraylist;
import java.util.list;
public class mythreadsuspend
{
public static void
main(string a[])
{
list
list = new arraylist();
for(int
i=0;i<4 i="" span="">4>
{
exmpthread
et = new exmpthread(i+10);
list.add(et);
et.start();
}
for(exmpthread
et:list)
{
try
{
et.join();
}
catch (interruptedexception ex) { }
}
}
}
class exmpthread extends thread
{
private int
suspendcount;
public exmpthread(int
count)
{
this.suspendcount
= count;
}
public void run()
{
for(int
i=0;i<50 i="" span="">50>
{
if(i%suspendcount
== 0)
{
try
{
system.out.println("Thread
Sleep: " + getname());
thread.sleep(500);
}
catch (interruptedexception ex) { }
}
}
}
}
Java Thread Yield
package com.myjava.threads;
import java.util.arraylist;
import java.util.list;
public class mythreadyield
{
public static void
main(String a[])
{
list
list = new arraylist();
for(int
i=0;i<3 i="" span="">3>
{
exmpthread
et = new exmpthread(i+5);
list.add(et);
et.start();
}
for(exmpthread
et:list)
{
try
{
et.join();
}
catch (interruptedexception ex) { }
}
}
}
class exmpthread extends thread
{
private int stopcount;
public exmpthread(int
count)
{
this.stopcount
= count;
}
public void run()
{
for(int
i=0;i<50 i="" span="">50>
{
if(i%stopCount
== 0)
{
system.out.println("Stoping
thread: "+getName());
yield();
}
}
}
}
Write a simple generics class
package com.java2novice.generics;
public class mysimplegenerics
{
public static void main(string a[])
{
simplegeneric
sgs = new simplegeneric("java2novice");
sgs.printtype();
simplegeneric
sgb = new simplegeneric(boolean.true);
sgb.printtype();
}
}
class simplegeneric
{
private t objreff = null;
public simplegeneric(t param)
{
this.objreff
= param;
}
public t getobjreff()
{
return this.objreff;
}
public void printtype()
{
system.out.println("Type:
"+objreff.getclass().getname());
}
}
No comments:
Post a Comment