I want to display seconds on a jlabel using a Thread while i move this with directions key. I put one thread, but this runs very slow... take a much delay time to show the seconds.
Related
In a java EE application I have one scheduledExecutorService with 2 timers on it.
The first via scheduledExecutorService.scheduleAtFixRate(), the second via scheduledExecutorService.schedule().
The first will activate a runnable every 2 seconds, the second will end after 60 seconds.
After the 60 seconds the scheduledExecutorService is stopped, so both timers are gone.
All is working fine with system.out statements in de runnables. But when I put a database read in the runnable of the timer on the 2 second interval the runable is activated only once and looks like to be pausing, the system.out with the read-result is not displayed and schedule is not running the runable every 2 seconds.
After the 60 second, the scheduledExecutorService is stopped and after that the system.out with the result of the database read is shown.
Any idea what may be causing this behaviour?
NB: Based on google-searches I have surrounded the database-read with a try catch catching Throwable but no Throwable is catched...
At first the result of the database was not shown, but that was solved by putting #Stateless and #TransactionType in the class from with the read is actually called.
The actual functionality I try to accomplish is to have two "timers" running. One on a interval of 2 seconds, one only as a count-down max waiting time.
In the 2 second intervals a database check is done to see if a certain value becomes true. If that's the case both timers need to stop and the calling program may continue. If the certain value stays false all the time the calling program may continue if the time of the count-down is done.
I configured Java based Selenium WebDriver test in Apache JMeter with the following setup:
Number of Threads (Users): 10
Ramp-up period (Second): 120
Loop Count: 1
I ticked the Delay Thread Creation until needed to save resources.
My expectation regarding the functionality:
I expected that if I have 10 users with 120 seconds ramp up time, then every user activity will start each other and the Jmeter will wait at least 12 seconds to start the next thread.
The issue is:
The threads start sometimes within 11 seconds, sometimes 12 seconds.
I don't know why does it happen because I would like to see the threads start after each other exactly in 12 seconds.
The question is
Are there any solution that to tell the JMeter to wait exactly 12 seconds for next thread start?
Here is the picture about started jobs with date time stamp:
I don't think you will be able to achieve this level of precision using ramp-up period approach of the normal Thread Group, a better idea would be going for the Ultimate Thread Group (can be installed using JMeter Plugins Manager) which allows absolute flexibility in terms of definition of ramp-up, ramp-down and time to hold the load.
Example setup:
Example output:
In order to get only one execution of the "job" per each virtual user you can use Throughput Controller configured like:
You can add Flow Control Action for pausing exact time
it allows pauses to be included without needing to generate a sample. For variable delays, set the pause time to zero, and add a Timer as a child.
I'm building an app and at one point in the app I need to construct a tableView that contains 3787 items in it. (There's a search bar at the top so the user doesn't have to scroll all the way down). However it takes a good 5 seconds to insert the array into the tableview, leading to loading time when the app starts up or before going to that scene. Is there a way to trim this time down? I thought of multithreading and looked up Lua coroutines but don't completely understand the implementation to get them running asynchronously. Or how to have a loading bar while the table is loading. The table is in another scene so im using stoyboard.loadScene()
I see three options:
You load the table at app startup: this delays the startup, possibly significantly (5 seconds will be noticeable if otherwise it would be 1 second), and table may never be needed (if user doesn't go to that scene) but after that, the table is instantly ready for display
You load the table on demand (say when user clicks something): app startup fast, and table only loaded if needed, but this delays transition to scene that shows table so user may think GUI hung, so you need to let user know and provide progress indicator
You start loading table at startup in a separate thread, and most likely it will take more than 5 seconds for user to get to scene that shows table so the app startup will be fast AND it will appear to the user that table load is instantaneous when going to scene that shows table. However, there is a chance that user will try to go to that scene before the table has been completely loaded, in which case you need to provide some indication that GUI not hung but load in progress.
You just load the part of table that is visible. This may not be an option (for instance, you need to show table sorted, but database doesn't provide items with same sort so you need to load all items).
I get the impression that you can handle 1 and 2, and most likely #4 too. For 1 and 2 you need to provide some indication that a load operation is taking some time, but otherwise nothing too difficult. For 4 there is no progress needed but you need to determine which rows to load based on the "view" of table (subset of rows visible).
Option is technically more challenging. You are right that you should use coroutines. They are actually quite easy to use:
you create the coroutine at startup: thread = coroutine.create(loadTable)
loadTable should be designed to do only small chunks of work at a time, and yield in between chunks, such as
function loadTable()
...init...
coroutine.yield()
while haveMoreRows do
read 10 rows
coroutine.yield()
end
...cleanup...
end
your code resumes the thread repeatedly, until the thread dies: coroutine.resume(thread). A good place to do this would be in the enterFrame handler of corona's Runtime since this is called at every time frame.
function enterFrame(e)
if thread ~= nil then
if coroutines.status(thread) == 'dead' then
create table display so it is instantly available in table scene
if showing progress, hide it
thread = nil
else
coroutine.resume(thread)
end
end
In your scene transition (to the scene that shows the table), you should check if thread is not nil, if so then the load is not yet done so you show the message (in new scene) that table is loading; the message will get removed in the enterFrame as soon as load completed.
An important thing to know about a coroutine (cooperative thread) is that the threaded function can have multiple yield points; at the next resume, the function continues to execute from where it left off, with the correct local state.
Hopefully you have looked at http://lua-users.org/wiki/CoroutinesTutorial.
I have a large query that needs to be loaded into memory. This query takes about 30 seconds to open. It loads immediately after the application starts, only once. During this time the application hangs. What I need to do is to update a progress bar, during the 30 seconds. I've tried to create a new thread to update the progress bar, but it will only update after the query is opened. Can anyone point a simple way to do this?
I've created a thread class:
type
TMyThread = class(TThread)
private
fLowerLimit: Integer;
fUpperLimit: Integer;
I'm creating an instance of the thread class:
CountingThread := TMyThread.Create(0, 300, True);
CountingThread.Resume;
//
SplashDlg.Show;
Inside the thread I'm just updating the progress bar:
procedure TMyThread.UpdateMainThread;
begin
SplashDlg.ProgressBar1.Position:= SplashDlg.ProgressBar1.Position+1;
MyDebug('UpdateMainThread:'+ IntToStr(SplashDlg.ProgressBar1.Position));
end;
The thread hangs while the query is opening.
It's better to paste some code here if you wish to get a more exact answer.
There are 2 ways in witch you can process data and have the UI responsive.
You can use Application.ProcessMessages in your loop.
Or the thread solution that you already approached.
The problem is I don't think you have a loop you are using a component and if that component doesn't call a method to show a progress you can't estimate how much time the query will take to complete. So either you show a loading... screen while te query executes without a progressbar or you tell us more about what you use for the query.
Hope it helps
You're doing it the other way around.
The UI is bound to the application main thread, so you always take care of the UI in the main thread, and do the real job in a worker thread (from there the worker name).
The way you're asking the question, you're already dealing with threads, so, just change your point of view and perform the heavy SQL load in a secondary thread while keeping your UI responsive and updated in the, now non-busy, main thread.
As for the 30 seconds wait time, really, if you can't determine exactly the load time is better to not use a progress bar, since we all hate that liar bars who don't reflect the real state of things. Your bar sometimes will show 50% and suddenly goes to 100%... or it may reach 100% while in fact the real thing is going to take ages to complete (heavy load on the server, slow network, and 1,000 other factors).
Nowadays, we all are used to the just wait indicators, like this:
When you see this, you know it is working, and you just have to wait until it completes.
I have an application with a lot of screens (followed by MVC pattern), and I want to be able to receive in a fashion way the information that last key was pressed x seconds ago (120 sec let's say). Is there standard way to do this or I have to start a timer and every time when I pressed a key I have to override a variable and in the timer I have to check the difference time between that time and current time?
Yes, just record the system timer when a key is pressed.
long epoch = System.currentTimeMillis();
When a key is pressed again, you need to check the time difference to see how long it's been idle for.
If you need to trigger things without keypresses, then you need to start a thread which wakes now and again to check the elapsed time, and trigger an event of some kind when the time period has elapsed.