vampir Posted September 7, 2012 Posted September 7, 2012 Lets say i have got such code inside class: ScheduledFuture<?> thread; private void startThread() { thread = ThreadPoolManager.getInstance().schedule(new TestThread(), 5000); } private class TestThread implements Runnable { @Override public void run() { System.out.println(thread.equals(TestThread.this)); } } It prints false to console, any idea to check if saved in var thread is running thread?
0 Fanky Posted September 7, 2012 Posted September 7, 2012 class nameclass implements Runnable { namethread runner; public RunnableThread(String threadName) { namethread = new Thread(this, threadName); System.out.println(runner.getName()); namethread.start(); } public void run() { System.out.println(Thread.currentThread()); } } dunno if you wanna smth like this, didn't used eclipse,i may have miss some tokens,dunno ;p
0 vampir Posted September 7, 2012 Author Posted September 7, 2012 :O thx a lot for an idea, final version will be: Thread thread; private void startThread() { ThreadPoolManager.getInstance().schedule(new TestThread(), 5000); } private class TestThread implements Runnable { Thread current; private TestThred() { current = Thread.currentThread(); thread = current; } @Override public void run() { System.out.println(thread.equals(current)); } } thx, i think its the best way to achieve that :)
0 Fanky Posted September 7, 2012 Posted September 7, 2012 glad that i helped :P may i ask why you need it?;p
0 vampir Posted September 7, 2012 Author Posted September 7, 2012 the problem in this code, is that thread nor the current variable cannot be stopped at all, i tried interrupt() and also stop() methods but finally System.out.println(thread.equals(current)); is done every time. I tried also to make new method in TestThread class to set "finished" boolean as false and later put IF before print but i cannot cast TestThread into a Thread class :/
0 Tryskell Posted September 8, 2012 Posted September 8, 2012 To stop a Thread from ThreadPoolManager, it's .cancel(boolean) *AND* put the task to null, aka "_task = null;".
0 vampir Posted September 8, 2012 Author Posted September 8, 2012 To stop a Thread from ThreadPoolManager, it's .cancel(boolean) *AND* put the task to null, aka "_task = null;". cancel method is inside ScheduledFuture or Future classes, not Thread :)
0 Tryskell Posted September 8, 2012 Posted September 8, 2012 cancel method is inside ScheduledFuture or Future classes, not Thread :) I never said that, I just said the correct mean to stop a thread, as you seem to get issues as you do weird things about comparing threads which is a nonsense until you got an issue with threads which made me thought about saying to you the correct way to stop a thread. See, the snake eats his tail.
0 vampir Posted September 8, 2012 Author Posted September 8, 2012 I resolved it in different way. When player is cancelling other player buffs, new Thread starts and it is added to a List of Threads. When Thread is finished, i am deleting it from list by checking Thread.currentThread() as before. I need the list because sometimes i want to cancel all Threads and clear the list. But i thought that i will not cancel them, but when thread starts to work(after sleeping :P) he will just check at the beggining, if its on the list. Thx for help :)
0 Zoubis®™ Posted September 8, 2012 Posted September 8, 2012 guys dont laugh at me..but i have to make a suggetion.what if you type break; under System.out.println(thread.equals(TestThread.this));
0 vampir Posted September 8, 2012 Author Posted September 8, 2012 guys dont laugh at me..but i have to make a suggetion.what if you type break; under System.out.println(thread.equals(TestThread.this)); u will have error :P break can be used only inside switches and loops and anyway it wouldnt change anything :)
0 Zoubis®™ Posted September 8, 2012 Posted September 8, 2012 u will have error :P break can be used only inside switches and loops and anyway it wouldnt change anything :) your right bro sorry im just reading e-books....so i just read it bellow :P
0 Tryskell Posted September 8, 2012 Posted September 8, 2012 I resolved it in different way. When player is cancelling other player buffs, new Thread starts and it is added to a List of Threads. When Thread is finished, i am deleting it from list by checking Thread.currentThread() as before. I need the list because sometimes i want to cancel all Threads and clear the list. But i thought that i will not cancel them, but when thread starts to work(after sleeping :P) he will just check at the beggining, if its on the list. Thx for help :) A list of threads ? Gods. Help. Him. Take a look on AttackStanceTaskManager... A unique thread with a unique map. On aCis, I did PvPFlag, Watertask and TakeBreak tasks looking as AttackStance.
0 vampir Posted September 8, 2012 Author Posted September 8, 2012 A list of threads ? Gods. Help. Him. Take a look on AttackStanceTaskManager... A unique thread with a unique map. On aCis, I did PvPFlag, Watertask and TakeBreak tasks looking as AttackStance. thats good idea, dunno why i didnt thought about that before, thx. Locking it :)
Question
vampir
Lets say i have got such code inside class:
It prints false to console, any idea to check if saved in var thread is running thread?
13 answers to this question
Recommended Posts