In fact, development has such an experience, that is, in daily work, in the face of some functions will always have some brain pumping things, perhaps clearly call a very simple method can be done, it is hard to achieve around a large circle.
Here I met a requirement, that is, a module, users click to generate a temporary experience to the user experience. The experience time is 20 minutes, and the permission is automatically closed after 20 minutes.
Without involving high concurrency and large traffic, my previous design was to add a timing scheduling method, refresh every second, and turn off expired user privileges. This method may be possible, but a scheduling method is executed every second, which is a waste of resources to be honest.
In fact, we can directly use thread dormancy to achieve the corresponding function of a new thread. Demo is roughly as follows:
Simulation method
import java.text.SimpleDateFormat; import java.util.Date; /** * Timing method callback * @author Administrator * */ public class Demo1 { public static String s(int i) { String s=i+"Starting time of No."+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); new Thread() { public void run() { try { sleep(5000); System.err.println(i+"Number execution time"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); return s; } }
public class Demo { public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 3; i++) { Thread.sleep(2000); System.out.println(Demo1.s(i)); } } }
Of course, someone might say, "Don't you mislead others? What if you have a lot of access and your crazy new threading system crashes?
Uh huh! Good question. So we can choose to use thread pools to optimize the Demo.
The known thread pools have the following categories
New CachedThreadPool creates a cacheable thread pool, which can flexibly reclaim idle threads if the length of the thread pool exceeds the processing requirement, and new threads if it is not.
New Fixed ThreadPool creates a fixed-length thread pool that controls the maximum number of concurrent threads, and the threads that exceed it will wait in the queue.
New Scheduled ThreadPool creates a fixed-length thread pool that supports both timing and periodic task execution.
New Single ThreadExecutor creates a single threaded thread pool, which only uses a single worker thread to perform tasks, ensuring that all tasks are executed in a specified order (FIFO, LIFO, priority).
After all, the project is not big, and I don't know the peak user usage, so let's use the first one here.
Optimized invocation method
import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Timing method callback * @author Administrator * */ public class Demo1 { static ExecutorService fixedThreadPool = Executors.newCachedThreadPool(); public static String s(int i) { String s=i+"Starting time of No."+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); fixedThreadPool.execute(new Runnable() { @Override public void run() { try { Thread.sleep(5000); System.err.println(i+"Number execution time"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } catch (InterruptedException e) { e.printStackTrace(); } } }); return s; } }
Above is the general Demo of executing one method regularly and then another method.
There is also the use of timing scheduling method, I have not tried so I can not, paste an address may be able to use: https://www.cnblogs.com/0201zcr/p/4703061.html