Reputation: 28
I don't know how to do this, what i am trying to do is running a for loop and still can do other things like reading input from user,change files contents, etc...(you get the idea) while the loop is running, is there any way to do it? Btw i am using java
Upvotes: 0
Views: 110
Reputation: 1433
You can Do it like this:
// the Thread class
class ThreadDemo extends Thread
{
public void run()
{
try
{
//Loop code
for(int counter = 0; counter < x ;counter++){
}
}
catch (Exception e)
{
// Throwing an exception
System.out.println ("Exception is caught");
}
}
}
// Main Class
public class Multithread
{
public static void main(String[] args)
{
ThreadDemo object = new ThreadDemo();
object.start();
}
}
More on Threads
Upvotes: 1
Reputation: 1684
You can do that using threads in java. Java don't support callbacks but with threads you can run multiple process at the same time. You can read more about threads in the java doc.or any java tutorial. The while loop should run on a separate thread while other things you want to do should run on another thread.
Upvotes: 1
Reputation: 120
You can create a new Thread for the task you want to do separately while the loop is running.
Thread t = new Thread(() -> {
//your code here
});
t.start();
Upvotes: 2