Cannot Schedule an Object that Extends BukkitRunnable a Second Time after Canceling - scheduler

So it seems that I can't schedule an object that extends bukkitrunnable again even though the first scheduled instance is canceled.
for example, the following code is executed when the Player right clicks
try {
blizzard.cancel();
}
finally {
blizzard.runTaskTimer(Main.plugin, 0, 1);
}
Where blizzard is an object that extends BukkitRunnable
The first time runs just fine but never runs again no matter how many times I right click. What am I doing wrong?
**Each player can have their own blizzard object. When a player right clicks with a hoe in hand, Blizzard drops a snowball at a random location around that player. Blizzard drops one snowball every run command and runs until it reaches a set number of runs. Then blizzard cancels itself. However, scheduling Blizzard to run while it is already running throws an IllegalStateException, meaning I have to cancel the current Blizzard to schedule it to run again. The problem is that doing so doesn't seem to properly schedule Blizzard to run again - it only runs the very first time it is scheduled.

By default, Bukkit does not allow runnables to be rescheduled even after they are cancelled. If you learned about Threads then you will remember that when a Thread is terminated, it can not be restarted. Bukkit follows this same logic when it comes to Runnable instances. A terminated (cancelled) Runnable can not be restarted. You must create a new Runnable instance if you wish to have that event occur again.
To have a Runnable run multiple times, you are on the right track. The BukkitScheduler.runTaskTimer method will run a task repeatedly until it is cancelled. If in your Listener you run Bukkit.getScheduler().runTaskTimer(Main.plugin, new Blizzard(), 0, 1); your Blizzard object will run repeatedly until it cancels itself. (You can also use blizzard.runTaskTimer(Main.plugin, 0, 1); from within the Blizzard constructor to automatically schedule the Runnable when it is created.)
If you keep an internal counter for how many times it has run, and increment that timer every run() method, you can cancel your Runnable after it has run a certain amount of times.

Related

Thread.sleep() proving superior to implicitlyWait

I have a lot of Thread.sleep() commands in my java selenium webdriver project. I have read often and again that using the sleep command is a bad practice and implicitlyWait command should instead be declared once after a browser instance is launched and it will take care of the whole script. This is not working for me on this project.
As There are a lot of NoSuchElementExceptions being thrown for certain elements, I added a sleep command just before each element that poses a problem. The script passed. When I took off the sleep commands and declared implicitlyWait 30 seconds, script failed with a WebdriverException/NoSuchElementException as webdriver is unable to find each of those elements. Sleep command is finding the elements in 2 or 3 seconds. Is Thread.sleep() proving superior here? If I have to use ExplicitlyWait, then I have to declare a lot of them also, as there are many elements that require expected conditions. Is there a workaround?
Thread.sleep() - Set each time you want to wait. A "dumb" wait. It waits for the time specified, no more, no less. If your element is available in 25ms, it will still wait the 10s that you specified leading to wasted time each run.
Implicit wait - Set once and persists throughout the life of the WebDriver instance. It waits for the specified time for every .findElement() call. So if you ever need to check that an element does not exist, etc. then your check will wait for the time specified in the implicit wait which also leads to wasted time.
Explicit wait (WebDriverWait) - Set each time you want a wait. This is just a specific instance of a FluentWait. It is customizable with all the common needs (visible, clickable, etc.) using ExpectedConditions. This is the best practice.
Fluent wait - Set each time you want a wait. The Swiss Army knife of waits. Can do anything... is fully customizable. Can be useful in very specific instances but generally most of your wait needs will be covered by WebDriverWait with ExpectedConditions.
How I use waits...
I use the page object model and in the constructor for each page object that is run when a page is loading, I use a WebDriverWait to wait for an element on the page to be available. Once that element is available, I know that the full page is loaded. Now I can scrape whatever elements I want off the page. If I ever change the state of the page, I have another WebDriverWait to wait for a new element to appear which signals that the dynamic page change has finished. Now I can scrape at will again.
You really shouldn't need to wait for every .findElement() call. Wait for the page to load or wait for a dynamic page change as I described above and you'll only need waits in very specific instances... not everywhere.
NOTE: Do not mix implicit and explicit waits. You will get bad/weird results.
Use explicit wait for your element to locate. It will wait till the condition or the maximum wait time provided before throwing the Exception.
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20); // pass time in seconds as much you need
WebElement myElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("your element's id")));
Note:
FluentWait may be another good option too.

Will Quart Scheduler Start executes the Paused Jobs?

Hi I'm new to Quartz Scheduler, i'm implementing it for the first time. I would like to know if the scheduler's start call will even execute the paused jobs? or Paused jobs will only be activated by resume call alone and nothing else. Please help me.
First of all, you can pause a trigger or scheduler, not a job. Do not be confused, that for example IScheduler interface has different pause methods, and one of them for example is PauseJobs(), they all affect triggers:
IScheduler.PauseAll: Pause all triggers - similar to calling PauseTriggers(GroupMatcher) on every group, however, after using this method ResumeAll() must be called to clear the scheduler's state of 'remembering' that all new triggers will be paused as they are added.
IScheduler.PauseTriggers: Pause all of the ITriggers in the groups matching.
IScheduler.PauseJobs: Pause all of the IJobDetails in the matching groups - by pausing all of their ITriggers.
IScheduler.PauseJob: Pause the IJobDetail with the given key - by pausing all of its current ITriggers.
The difference between Pause and StandBy methods may help you to understand what and when will be started:
The Pause methods are used to pause all the existing triggers you have added to scheduler only. This affect existing triggers only, and scheduler is still RUNNING! Any new job/trigger added will still run, and it's not paused! You would have to call Resume to unflag those paused triggers to get them running again.
The Standby is used to place the entire scheduler in "not working" mode, meaning no jobs or triggers will be fire or run, including ones already scheduled, or new ones added. Not until you call Start again.

can a timer trigger during another timer's callback?

I have two timers running simultaneously. The first timer triggers every 1 second and takes 0.2 seconds to run. The second timer triggers every 20 minutes and takes 5 minutes to run. I would like to have the first timer continue triggering during the 5 minutes it takes the second timer execute its callback. In practice, during the second timer's callback the first timer does not trigger. Is it possible to configure the timers to execute the way I want?
There is a workaround, depending on how your timer callbacks' work is structured. If the long timer callback is running a long loop or sequence of calls to different functions, you can insert drawnow() or pause(0.01) calls to make it yield to Matlab's event dispatch queue, which will handle pending handle graphics and timer events, including your other Timer's trigger.
It's sort of like old-school cooperative multitasking where each thread had to explicitly yield control to other threads, instead of being pre-empted by the system's scheduler. Matlab is single-threaded with respect to M-code execution. When a Matlab function is running, events that get raised are put on an event queue and wait until the function finishes and returns to the command prompt, or drawnow(), pause(), uiwait() or a similar function is called. This is how you keep a Matlab GUI responsive, and is documented under their Handle Graphics stuff. But Matlab timer objects use the same event queue for their callbacks. (At least as of a couple versions ago; this is only semi-documented and might change.) So you can manage their liveness with the same functions. You may also need to tweak BusyMode on your timers.
This is kind of a hack but it should get you basic functionality as long as you don't need precise timing, and don't need the callbacks' code to actually run in parallel. (Whichever timer callback has yielded will wait for the other one to finish before proceeding with its own work.)
If the long callback is really blocked on a long operation that you can't stick drawnow calls in to, you're out of luck with basic Matlab and will need to use one of the workarounds the commenters suggest.

Remote debugging in eclipse in multithreaded environment

I am doing remote debugging using eclipse. My requirement is to make 20 requests at the same time, stopping at one point using debug breakpoint and then release all the suspended threads at the same time to test how code is behaving when multiple threads access the code at same time. However, when I tried this I found only one thread is serving all the request
Daemon Thread [http-0.0.0.0-8080-Processor60] (Suspended (breakpoint at line 440 in VcsInfoDAO))
when first request completes, then only second request comes to the breakpoint serving by the same thread mentioned above. Is there any setting in eclipse to make it all the request comes to a single point and then in some way to release the threads at the same time so that all the threads access the code thereafter at the same time.
Any help would be highly appreciated.
Sourabh
Eclipse has nothing to do with what you see. If you set a breakpoint to some place inside a method supposed to be called concurrently, and if your client code really launches 20 concurrent requests, and if you observe that the second request is only handled once the first one has finished, then what you thought was concurrent is not.
I see two possible explanations:
you have a unique thread handling all the requests. If several are sent concurrently, all the requests are queued and handled one by one
you have several threads handling the request concurrently, but the client code sends 20 requests sequentially, rather than sending 20 requests concurrently.
Anyway, using a breakpoint to test such a thing is not a good solution. You'll have to hit the "Continue (F8)" button for each of the 20 threads, thus they won't restart at the same time. You'd bette use a CountDownLatch initialized at 20 to do that:
private CountDownLatch latch = new CountDownLatch(20);
public void run() {
// some code
// here we want to pause all 20 threads and restart them all at the same time
latch.countDown(); // The 20th thread will open the barrier, and they will all restart at the same time
latch.await();
}

How a runloop actually works

Earlier this month I asked this question 'What is a runloop?' After reading the answers and did some tries I got it to work, but still I do not understand it completely. If a runloop is just an loop that is associated with an thread and it don't spawn another thread behind the scenes how can any of the other code in my thread(mainthread to keep it simple) execute without getting "blocked"/not run because it somewhere make an infinite loop?
That was question number one. Then over to my second.
If I got something right about this after having worked with this, but not completely understood it a runloop is a loop where you attach 'flags' that notify the runloop that when it comes to the point where the flag is, it "stops" and execute whatever handler that is attached at that point? Then afterwards it keep running to the next in que.
So in this case no events is placed in que in connections, but when it comes to events it take whatever action associated with tap 1 and execute it before it runs to connections again and so on. Or am I as far as I can be from understanding the concept?
"Sort of."
Have you read this particular documentation?
It goes into considerable depth -- quite thorough depth -- into the architecture and operation of run loops.
A run loop will get blocked if it dispatches a method that takes too long or that loops forever.
That's the reason why an iPhone app will want to do everything which won't fit into 1 "tick" of the UI run loop (say at some animation frame rate or UI response rate), and with room to spare for any other event handlers that need to be done in that same "tick", either broken up asynchronously, on dispatched to another thread for execution.
Otherwise stuff will get blocked until control is returned to the run loop.