echo "Doing complicated work {$i} / 10" . PHP_EOL;
            sleep(1);
        }
        echo "After complete the Thread is closed automatically" . PHP_EOL;
    }
}
$loop = ForkableFactory::create();
$thread = new EchoThread($loop);
//Let the thread work for some seconds then kill and start again
$loop->addPeriodicTimer(3, function () use($thread) {
    //check if thread is running
    if ($thread->isRunning()) {
        echo "Thread is Running... Kill it!" . PHP_EOL;
        //cancel everything the thread is doing
        $thread->kill();
    } else {
        echo "Thread is not running... Start Working!" . PHP_EOL;
        //cancel everything the thread is doing
        $thread->start();
    }
});
//stop process after 30 seconds, otherwise it runs forever
$loop->addTimer(30, function (TimerInterface $timer) use($thread) {
    //check if thread is running
    if ($thread->isRunning()) {
        //kill if its running for an clean shutdown
        $thread->kill();
    }
    $timer->getLoop()->stop();
});
$loop->run();