Thread by Implementing Runnable Interface
package com.myjava.threads;
class MyRunnableThread
implements Runnable
{
public static int myCount = 0;
public MyRunnableThread()
{
}
public void run()
{
while(MyRunnableThread.myCount
<= 10)
{
try
{
System.out.println("Expl
Thread: "+(++MyRunnableThread.myCount));
Thread.sleep(100);
}
catch (InterruptedException iex)
{
System.out.println("Exception
in thread: "+iex.getMessage());
}
}
}
}
public class RunMyThread
{
public static void main(String a[])
{
System.out.println("Starting
Main Thread...");
MyRunnableThread
mrt = new MyRunnableThread();
Thread
t = new Thread(mrt);
t.start();
while(MyRunnableThread.myCount
<= 10)
{
try
{
System.out.println("Main
Thread: "+(++MyRunnableThread.myCount));
Thread.sleep(100);
}
catch (InterruptedException iex)
{
System.out.println("Exception
in main thread: "+iex.getMessage());
}
}
System.out.println("End
of Main Thread...");
}
}
Java Thread by Extending Thread
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...");
}
}
Daemon Thread
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();
}
}
Thread Join
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());
}
}
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) { }
}
}
}
}
No comments:
Post a Comment