import std.thread;
import std.stdio;
import std.c.stdio;
import std.date;
// Thread proses utama ....
class mythread:Thread
{
private char [] d_name = "";
private long d_nloop = 10;
private bool d_stop = false;
this(char [] name, long nloop)
{
super();
writefln(name ~ " called");
this.d_name = name;
this.d_nloop = nloop;
}
override public int run()
{
long i = 0;
for(i=0;(i<this.d_nloop && (!d_stop));i++)
{
//writefln(d_name ~ " called again ");
}
if( d_stop )
writefln(d_name ~ " stopped by user in "
~ std.string.toString(i) ~ " times of loop");
else
writefln(d_name ~ " Finished ..." ~ std.string.toString(d_nloop));
return 0;
}
public void stopIt()
{
d_stop = true;
}
}
// thread untuk menunggu pengguna menekan q atau Q
class UserToStopAllThread: Thread
{
private bool d_stop = false;
this()
{
super();
}
override public int run()
{
while(!d_stop)
{
char ch = getch();
if(ch == 'q' || ch == 'Q')
break;
}
return 0;
}
public void stopIt()
{
d_stop = true;
}
}
// ini adalah Thread utama ....
void main(char [][] args)
{
int i = 0;
mythread a = new mythread("A", 9999999999999);
writefln("Tekan q untuk menghentikan proses ....");
UserToStopAllThread utsa = new UserToStopAllThread;
a.start();
utsa.start();
while(a.getState() == Thread.TS.RUNNING && utsa.getState() == Thread.TS.RUNNING)
{
// tunggu sampai a selesai atau user menekan tombol 'Q' or 'q'
}
if(a.getState() == Thread.TS.RUNNING) a.stopIt(); // pastikan berhenti
if(utsa.getState() == Thread.TS.RUNNING) utsa.stopIt(); // pastikan berhenti
}
0 komentar: