The best way to save the main method

I used a way to make the main method work.

public static void main(String[] args) throws InterruptedException { while (true) { TimeUnit.SECONDS.sleep(1); } } 

But I'm not sure if this is the best way.

Can someone give me some advice?

+5
source share
4 answers

The best way is to hold the main thread () without transitioning to the "completed / dead" state. Below are two methods that I often use use-

  public static void main(String[] args) throws InterruptedException { System.out.println("Stop me if you can"); Thread.currentThread().join(); // keep the main running } 

Another way is to create a ReentrantLock and call the wait () function on it:

 public class Test{ private static Lock mainThreadLock = new ReentrantLock(); public static void main(String[] args) throws InterruptedException { System.out.println("Stop me if you can"); synchronized (mainThreadLock) { mainThreadLock.wait(); } } 
0
source

You must use the ExecutorService and submit a task waiting to read your services.

 package demo; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; public class TaskDemo { public static void main(String[] args ) { System.out.println("Hello"); ExecutorService threadPool = Executors.newFixedThreadPool(1); Runnable task = () -> { try { //Loop to read your services output Thread.sleep(1000); System.out.println("This is from Task"); } catch(InterruptedException e) { e.printStackTrace(); } }; threadPool.execute(task); //Wait for the task to finish and shutdow the pool threadPool.shutdown(); } } 
-2
source

You have many options, some of them:

  • Simple theme
  • Timertask
  • ScheduledExecutorService

Simple topic:

 public class Task { public static void main(String[] args) { final long timeInterval = 1000; Runnable runnable = new Runnable() { public void run() { while (true) { System.out.println("Running Task ..."); try { Thread.sleep(timeInterval); } catch (InterruptedException e) { e.printStackTrace(); } } } }; Thread thread = new Thread(runnable); thread.start(); } } 

TimerTask:

  import java.util.Timer; import java.util.TimerTask; public class Task { public static void main(String[] args) { TimerTask task = new TimerTask() { @Override public void run() { System.out.println("Running Task ..."); } }; Timer timer = new Timer(); long delay = 0; long intevalPeriod = 1000; timer.scheduleAtFixedRate(task, delay,intevalPeriod); } } 

ScheduledExecutorService:

 import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Task { public static void main(String[] args) { Runnable runnable = new Runnable() { public void run() { System.out.println("Running Task ..."); } }; ScheduledExecutorService service = Executors .newSingleThreadScheduledExecutor(); service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS); } } 
-2
source

Here is one way to keep the main java instance to run until you want to exit. To do this, use the graphical interface:

 public static void main(String[] args){ JFrame jFrame = new JFrame(); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setLocationRelativeTo(null); jFrame.setSize(480, 260); jFrame.setVisible(true); //do some task here } 
-3
source

Source: https://habr.com/ru/post/1276015/


All Articles