Implementing a delay without thread usage - java

I'm working on a little project where I want to implement a little delay in a loop, but I don't want to use a thread or use another class. Is this possible?
My code looks like this:
int random = (int)(Math.random()*15);
int randomloop;
for (i = 0; i < random; i++){
randomloop = (int)(Math.random()*15);
nummerlabel.setText(String.valueOf(randomloop)); //showing a number
//here i would like to implement a little delay, so the number changes every 0.5 seconds
}

Seeing as you are already using Math and String I am assuming you are not opposed to using the standard library and you simply want to avoid creating a new thread. In that case, use Thread.sleep(...).

Related

How do I parallellise a for loop, and save the results of each repetition in a matrix?

I'm running a simulation for several repetitions and I'm trying to see if it would be possible to parallellise these repetitions to improve computation time.
Currently I simply run the simulation several times in a for loop, but ideally these could all be run at the same time, and then the results from each repetition saved to an array for processing.
Currently my code is something like
public static void getAllTheData(){
int nReps = 10;
double[][] allResults = new double[nReps][]
//this for loop is what I want to parallellise
for(int r = 0; r < nReps; r++){
//run the simulation to completion here
simulation.doStuffToCompletion()
allResults[r] = simulation.getValues()
}
//average allResults here and do further analysis
}
My question is, how can I run all these repetitions at the same time in parallel, and save the results from each parallel run in an allResults type array?
Thanks very much.
You can try something like parallel streams to do parallelism like one of the below lines.
Stream.of(allResults).parallel().forEach(i -> processData(i));
Arrays.stream(allResults).parallel().forEach(i -> processData(i));
Java is a synchronous language by default and to do work in parallel you would need to look into threads. I suggest looking into asynchronous function calls and ways to do it in Java.
Here is a previous question asked on this matter: How to asynchronously call a method in Java
With an asynchronous call, you can run your simulation on however many threads you want and collect the data as your simulation runs finish.

How to use the while loop as a way to "pause" a process? (Using loop as an alternative to Threading)

I want to allow the process new LongDistanceCalls() to finish creating an instance first before continuing on the rest of program. However I am stuck and my solution so far is the following:
int count = 1;
do
{
new LongDistanceCalls();
count++;
}while(count ==1);
new LongDistanceCallsTWO();
Is it possible to use loop to do this? Thank you.

Infinite scroll like behaviour on loop

I have in my android app a code snippet that uses a loop for making/drawing rectangles.
int total_rect = 2000;//This nr should not have to be here.
float rect_positions[][] = new float[total_rect][2];
for(int i =0;i<total_rect;i++){
rect_positions[i][0] = 600 + i*300 ;
rect_positions[i][1] = rand.nextFloat() * 0.8f + 0.1f;
}
They appear every 300 DP when the game scrolls.
The reason is that i wanted it to be endless of rectangles so i dont want to set a specific nr to it.
But i have(?) to make an initial nr to run the loop. (not really sure about this...)
So what i want to know is:
Is there any other(better) way to do this because now i get REALLY low fps and it is lagging like h3ll due to all the calculations for the loop.
Maybe something in the lines of :
int total_rect = 5;
when i>4 run loop again //This is what i dont know how to code...
That should work i think.
Or maybe something like
when rect_positions>last_rectposition + 1500 run loop again //This is the one i think would be the best alternative because that would meen that the further you get the more loops it will be.
Or is it anyone that have a different idea?

java for loop executes too fast gives System.currentTimeMillis() duplicate

Java: I have a problem using System.currentTimeMillis() function
i am using System.currentTimeMillis() to generate unique values in foor loop problem is loop executes too fast and System.currentTimeMillis() gives me duplicate values.
How can i generate for sure unique values.
for(int a=0;a<=10;a++){
System.out.println(System.currentTimeMillis())
}
I also tried following but it is also not generaet to generate unique number
System.currentTimeMillis()+Math.random()
why don't you use System.nanoTime() instead?
Why don't you use a UUID library to generate unique identifiers (already there in the JDK http://download.oracle.com/javase/6/docs/api/java/util/UUID.html).
Or for a more simple approach: append a static counter
I think your approach is wrong, if this is a requirement.
Theoretically, no matter how fine-grained your timer, a machine might execute it in less time than the timer's granularity. It's not correct in a technical sense to depend on this being true.
Or looking at it another way - why do you need these values to be unique (what are you using them for)? If you really want them to be a measure of the time it was executed, then you ought to be happy that two iterations that happened within the same millisecond got the same value.
Have you considered using a static, monotonous counter to assign IDs to each iteration that are unique within each execution (AtomicLong is great for this)? Something like the following is very easy and has no concurrency issues:
public class YourClass {
private static final AtomicLong COUNTER = new AtomicLong();
private static nextId() { return COUNTER.getAndIncrement(); }
// Rest of the class, which calls nextId() when it needs an identifier
}
If you need the timing info and uniqueness, then that's two separate requirements, so why not have a composite key made up of the time and an arbitrary unique ID?
The answer is obvious - get a slower computer! Well, that or use System.nanoTime as described right here on SO - System.currentTimeMillis vs System.nanoTime. But seriously, you shouldn't be using time as unique number generator unless you absolutely have to.
The problem with using the system time of course being that:
The time returned by your system
calls is rounded up to a higher
degree of precision than the actual
CPU clock time. If your ID generation
code runs faster than this degree of
precision then you will have
collision.
If your code is distributed and each
unit of work is generating ID's then
you run into the possibility of ID
collision as the separate CPU's or
CPU core's allocate ID's using their
independent clocks.
In libraries like Java that are
actually returning the system time
based off a user settable property
you run into a higher chance of
multiple ID collision anytime the
date is reset to some period in the
past, for whatever reason.
A very good alternative to generating unique identifiers is to utilize the not-so-ironically named Universally Unique Identifier. There is a multiple implementations in various languages, for Java 5 and higher you can use the UUID class.
Edit: To add some useful information about UUID.
Similar to #Andrej's solution, but combining a timer and a counter so your numbers shouldn't repeat if you restart your application.
public enum IdGenerator {
;
private static final AtomicLong COUNTER = new AtomicLong(System.currentTimeMillis()*1000);
public static long nextId() { return COUNTER.getAndIncrement(); }
}
If you want to still use your method, you could do:
for(int a=0;a<=10;a++){
Thread.sleep(1);
System.out.println(System.currentTimeMillis())
}
Explicitly making your CPU slower.
try Math.random()*System.currentTimeMillis()
here is a sample outcome
4.1140390961236145E11,
4.405289623285403E11,
6.743938910583776E11,
2.0358542930175632E11,
1.2561886548511025E12,
8.629388909268735E11,
1.158038719369676E12,
2.5899667030405692E11,
7.815373208372445E11,
1.0887553507952611E12,
3.947241572203385E11,
1.6723200316764807E11,
1.3071550541162832E12,
2.079941126415029E11,
1.304485187296599E12,
3.5889095083604164E10,
1.3230275106525027E11,
6.484641777434403E11,
5.109822261418748E11,
1.2291750972884333E12,
8.972865957307518E11,
4.022754883048088E11,
7.997154244301389E11,
1.139245696210086E12,
2.633248409945871E11,
8.699957189419155E11,
9.487098785390422E11,
1.1645067228773708E12,
1.5274939161218903E11,
4.8470112347655725E11,
8.749120668472205E11,
2.435762445513599E11,
5.62884487469596E11,
1.1412787212758718E12,
1.0724213377031631E12,
3.1388106597100226E11,
1.1405727247661633E12,
1.2464739913912961E12,
3.2771161059896655E11,
1.2102869787179648E12,
1.168806596179512E12,
5.871383012375131E11,
1.2765757372075571E12,
5.868323434343102E11,
9.887351363037219E11,
5.392282944314777E11,
1.1926033895638833E12,
6.867917070018711E11,
1.1682059242674294E12,
2.4442056772643954E11,
1.1250254537683052E12,
8.875186600355891E10,
3.46331811747409E11,
1.127077925657995E12,
7.056541627184794E11,
1.308631075052609E12,
7.7875319089675E11,
5.52717019956371E11,
7.727797813063546E11,
6.177219592063667E11,
2.9448141585070874E11,
9.617992263836586E11,
6.762500987418107E11,
1.1954995292124463E12,
1.0741763597148225E12,
1.9915919731861673E11,
9.507720563185525E11,
1.1009594810160002E12,
4.1381256571745465E11,
2.2526550777831213E11,
2.5919816802026202E11,
3.8453225321522577E11,
3.796715779825083E11,
6.512277843921505E10,
1.0483456960599313E12,
1.0725956186588704E11,
5.701504883615902E11,
9.085583903150035E11,
1.2764816439306753E12,
1.033783414053437E12,
1.188379914238302E12,
6.42733442524156E11,
3.911345432964901E11,
7.936334657654698E11,
1.4473479058272617E11,
1.2030471387183499E12,
5.900668555531211E11,
8.078992189613184E11,
1.2004364275316113E12,
1.250275098717202E12,
2.856556784847933E11,
1.9118298791320355E11,
5.4291847597892596E11,
3.9527733898520874E11,
6.384539941791654E11,
1.2812873515441786E11,
6.325269269733575E9,
5.403119000792323E11,
8.023708335126083E11,
3.761680594623883E10,
1.2641772837928888E11,
Check out UUID as well...
My suggestion
long id = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
//do your work
id++;
}

Android Editable CountDownTimer Issue

I'm trying to implement a CountDownTimer in the .java of an Android app's activity. This CountDownTimer needs to be able to count down from several different pre-set timings that is specified by the user's selection, that which I have solved by using sharedPreferences().
In order to connect the sharedPreferences and the CountDownTimer's timing, below are two method's I've tried that do not work:
1) getInt from the sharedPreferences and then defining an string array and substituting long milliseconds (for the CountDownTimer) with a long value from the array by using array[int] and thereafter valueof() to that obtained string.
2) By using {if(sharedPreferences == 123) {int timing = 5000;}}, but this fails due to the braces that contain the value of int timing such that CountDownTimer cannot read it as it lies outside of that pair of braces that contain the if. Worth mentioning is that it is not possible to move the CountDownTimer into the braces.
Quite simply, I'm looking for a solution to an editable CountDownTimer. While I'm not asking for a whole tutorial right here on Stackoverflow, it would be nice if you could point me in the right direction.
I don't really understand why your approaches don't work because I'm having a problem picturing your code. In general, if you declare int timing = 0; inside an if-clause it will only be available within this if-clause. So, to make it available on the outside you also need to declare it there.
I'd start with something like this:
int timing = 0;
switch (sharedPreferences) {
case 123:
timing = 5000;
case 124:
timing = 10000;
case 125:
timing = 15000;
...
default:
timing = 20000;
}
startMyCountdown(timing);

Categories

Resources