In general, I have a cycle of forts, in a cycle of a random boolean generator, true or false, if true, then variable i displays a series of numbers in the interval. So how can you define this same loop in a method so that I can run a lot of its instances somewhere from the code while getting the value of the variable i?

for (int op = 0; op>0; op++) { boolean bo = random.nextBoolean(); if (bo == true) { for ( i = 0; i < 5; i++) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(i); } } else { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } 

That is, for example, I have several conclusions from System.out.Println (), and each of them must launch a new instance of this method and receive different values ​​of the variable. Not necessarily on Println (), I can drawText () for this purpose. For me, the main thing is to understand this example. Thank!

  • You have an endless loop - Flippy
  • one
    On the contrary, the beginningless one is vp_arth
  • @ Sergey Grushin but infinite, that's what I need - Jonathan
  • @ Sergey Grushin for (int op = 0; op > 0; op++) loop will execute exactly 0 times. Therefore, it is difficult to call it infinite. - Regent

2 answers 2

First, use the construction of an infinite loop of this type, it is more concise and clearer:

  while (true) { ... } 

Secondly, if I correctly understood your task, you can try to do this: create a separate class with a loop in the method and for each class of the instance there will be its own such method:

 public class CyclerThread { public void startCycle() { //ТУТ ВАШ ЦИКЛ } } 

Use this:

 new CyclerThread().startCycle(); new CyclerThread().startCycle(); new CyclerThread().startCycle(); 

All three cycles will work simultaneously and independently of each other. Although, ideally, each one needs to be run in a separate thread.


To get the values ​​of i , you can pass either the interface to which it will be passed, or the method in the class in which it will be taken.

Option 1:

 public class CyclerThread { public interface Callback { void onValueChanged(int i); } public void startCycle(Callback callback) { ... if (ВАШЕ_УСЛОВИЕ) { callback.onValueChanged(i); } ... } } 

Option 2:

 public class CyclerThread { private int i; ... public int getValue() { return i; } } 

To get the answer, you need to pass an anonymous class to the method call:

  new CyclerThread().startCycle(new CyclerThread.Callback() { @Override public void onValueChanged(int i) { } }); 

Or using lambda:

 new CyclerThread().startCycle((i) -> ... ); 
  • Thank you so much for the answer, it really helped, but I still don’t understand one moment if I pass the value via callback.onValueChanged (i); - how can I take it in another class? tried new CyclerThread (). startCycle (int y); - int y but such a construction didn’t work if not strange - Jonathan
  • @Jonathan, updated the answer. If the answer helped you, please accept it. - Rostislav Dugin
  • yes of course, thank you again. - Jonathan
  • tried using lambda int y = 0; new CyclerThread (). startCycle ((i) -> i = y); canvas.drawText ("" + y, 100,100, paint); in the end, y = 0, like lambda works like this or am I mistaken somewhere ?! - Jonathan
  • @Jonathan, uh ... Not i = y , but y = i ... - Rostislav Dugin

You have described some logic. This logic needs to be placed in a separate class (some BooleanRandomGeneratorClassExample ).

And then create objects of this class that either will immediately print to the console or will return a collection or array with a set of random values ​​at the output.

Eg BooleanRandomGeneratorClassExample.java

 public class BooleanRandomGeneratorClassExample { public static List<Boolean> generateBooleanList(int size) { if (size < 1) { return null; } Random rand = new Random(); List<Boolean> list = new ArrayList<>(); for (int i = 0; i < size; i++) { list.add(rand.nextBoolean()); } return list; } } 

Using the BooleanRandomGeneratorClassExample class:

 public class Application { public static void main(String[] args) { List<Boolean> myList = BooleanRandomGeneratorClassExample.generateBooleanList(5); for (boolean bVal : myList) { System.out.println(bVal); } } } 

A variant of using the BooleanRandomGeneratorClassExample class with infinity:

  public class Application { public static void main(String[] args) { List<Boolean> myList = null; while (1 == 1) { myList = BooleanRandomGeneratorClassExample.generateBooleanList(5); for (boolean bVal : myList) { System.out.println(bVal); } } } } 

If parallel work of such a method is needed, then it is possible to implement the run() method in the BooleanRandomGeneratorClassExample class and run the BooleanRandomGeneratorClassExample class in multi-threaded mode.

  • Thanks for the rescue, you wrote that you can return an array with a set of values, and can you make it so that in each value of the array there is a new instance of the object? - Jonathan
  • Yes of course. You can create an array or collection of objects. List <YourClassName> list = new ArrayList <> (); or YourClassName [] myArray = YourClassName [10]; If it is not clear in the comments, then write an example which is needed - I will supplement my answer. P.S. The best thanks is +1 vote for the answer)) - Mikhail Grebenev
  • No, thanks again, everything is clear. Is that how to put a +1 vote) I've been here recently) - Jonathan