Handle Multiple Infinite Tasks In A Single Thread? P.s Run One Task At A Time And Control Its Task Behavior(i.e Starting/stoping Task) From Outside
I want to make a single thread which would contain 3 infinite tasks. I want one task to run at a time and start/stop running task when required. For example first I want task 1 to
Solution 1:
Use this for start/stop thread in real-time:
classMyThreadextendsThread {
privatevolatile boolean running = true; // Run unless told to pause
...
@Override
publicvoidrun() {
// Only keep painting while "running" is true// This is a crude implementation of pausing the threadwhile (true) {
if (Thread.currentThread().isInterrupted()) {
return;
}
if (running) {
//Your code
} elseyield;
}
}
publicvoidpauseThread() throws InterruptedException {
running = false;
}
publicvoidresumeThread() {
running = true;
}
}
For pause thread use this:
myThread.pauseThread();
For resume thread use this:
myThread.resumeThread();
For stop thread use this (Not recommended):
myThread.stop();
For currently stop thread use this:
myThread.interrupt();
Solution 2:
You must use a class like Thread that already implements Runnable.
new Thread(){....};
And the way it works it's:
Threadt=newThread(){.....};
t.start();
t.stop();
Solution 3:
You could also initialize a new thread, like:
ThreadexampleThread=newthread();
After this you can start it at any point in your code by:
exampleThread.start();
Solution 4:
you can use Semaphore, to Manage the amount of signal.
privatefinalstaticSemaphoresemaphore=newSemaphore(0);
publicstaticvoidmain(String[] args)throws Exception {
//入口
threadTest();
}
publicstaticvoidthread1() {
try{
//…… some code
}
finally{
semaphore.release();
}
}
publicstaticvoidthread2() {
semaphore.acquire(1);
}
The question is my first answer,thanks.
Solution 5:
I finally made my task scheduler. The API of which looks something like this:
TaskSchedulertaskScheduler= TaskScheduler.getInstance();
taskScheduler.startTaskOne();
taskScheduler.stopTaskOne();
taskScheduler.startTaskTwo();
taskScheduler.stopTaskTwo();
Runs one task at a time (because I used Executors.newSingleThreadExecutor()). We can control the execution of the task from outside:
publicclassTaskScheduler {
privatestatic ExecutorService mTaskRunningService;
privatestatic TaskScheduler mInstance;
privateFuturemFirstTaskFuture=null;
privateFuturemSecondTaskFuture=null;
static {
configure();
}
privatestaticvoidconfigure() {
mTaskRunningService = Executors.newSingleThreadExecutor();
}
publicstatic TaskScheduler getInstance() {
if (mInstance == null) {
mInstance = newTaskScheduler();
}
return mInstance;
}
privateRunnablemTaskOneRunnable=newRunnable() {
@Overridepublicvoidrun() {
try {
while (true) {
/** stop this single thread (i.e executing one task at time) service if this thread is interrupted
* from outside because documentation of {@link java.util.concurrent.ThreadPoolExecutor#shutdownNow()}
* says we need to do this*/if (Thread.currentThread().isInterrupted()) {
return;
}
// task one work.......
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
privateRunnablemTaskTwoRunnable=newRunnable() {
@Overridepublicvoidrun() {
try {
while (true) {
/** stop this single thread (i.e executing one task at time) service if this thread is interrupted
* from outside because documentation of {@link java.util.concurrent.ThreadPoolExecutor#shutdownNow()}
* says we need to do this*/if (Thread.currentThread().isInterrupted()) {
return;
}
// task two work......
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
publicsynchronizedvoidstartTaskOne() {
if (mFirstTaskFuture == null) {
// start executing runnable
mFirstTaskFuture = mTaskRunningService.submit(mTaskOneRunnable);
}
}
publicsynchronizedbooleanstopTaskOne() {
if (mFirstTaskFuture != null) {
// stop general reading thread
mFirstTaskFuture.cancel(true);
// cancel statusbooleanstatus= mFirstTaskFuture.isDone();
// assign null because startTaskOne() again be called
mGeneralFuture = null;
return status;
}
returntrue;
}
publicsynchronizedvoidstartTaskTwo() {
if (mSecondTaskFuture == null) {
// start executing runnable
mSecondTaskFuture = mTaskRunningService.submit(mTaskTwoRunnable);
}
}
publicsynchronizedbooleanstopTaskTwo() {
if (mSecondTaskFuture != null) {
// clear task queue
mTaskQueue.clearTaskQueue();
// stop 22 probes reading thread
mSecondTaskFuture.cancel(true);
// cancel statusbooleanstatus= mSecondTaskFuture.isDone();
// assign null because startTaskTwo() again be called
mSecondTaskFuture = null;
return status;
}
returntrue;
}
}
Post a Comment for "Handle Multiple Infinite Tasks In A Single Thread? P.s Run One Task At A Time And Control Its Task Behavior(i.e Starting/stoping Task) From Outside"