Here I will provide you an programming example which helps you understand how it works.
Code:
import java.lang.*;
import java.applet.Applet;
public class MultithreadingExample extends Applet
{
Example EXA;
public void start()
{
int i;
CDE=new Example();
for(i=0;i<20;i++) /* start 20 threads */
new Thread(EXA,new String("booThread"+i)).start();
}
public void stop()
{
EXA.stopThreads();
}
}
class MultithreadingExample extends Object implements Runnable
{
int num1=0;
public void run()
{
int i=0;
for(;;)
{
for(i=0;i<1000;i++)
{
num=num+10;
num=num-10;
}
try {Thread.sleep(10000);}
catch (InterruptedException e ) {}
System.out.println(Thread.currentThread().getName()+
" sees the number: " + num1);
}
}
void stopThreads()
{
Thread tArray[];
int numThreads;
numThreads=Thread.activeCount();
tArray=new Thread[numThreads];
Thread.enumerate(tArray);
for(int i=0;i<numThreads;i++)
if(tArray[i].getName().startsWith("booThread"))
tArray[i].stop();
}
}
Bookmarks