public synchronized final void run()

in wicket-util/src/main/java/org/apache/wicket/util/thread/Task.java [89:184]


	public synchronized final void run(final Duration frequency, final ICode code)
	{
		if (!isStarted)
		{
			final Runnable runnable = new Runnable()
			{
				@Override
				public void run()
				{
					// Sleep until start time
					Duration untilStart = Duration.between(startTime, Instant.now());

					final Logger log = getLog();

					if (!untilStart.isNegative())
					{
						try
						{
							Thread.sleep(untilStart.toMillis());
						}
						catch (InterruptedException e)
						{
							log.error("An error occurred during sleeping phase.", e);
						}
					}

					try
					{
						while (!stop)
						{
							// Get the start of the current period
							final Instant startOfPeriod = Instant.now();

							if (log.isTraceEnabled())
							{
								log.trace("Run the job: '{}'", code);
							}

							try
							{
								// Run the user's code
								code.run(getLog());
							}
							catch (Exception e)
							{
								log.error(
									"Unhandled exception thrown by user code in task " + name, e);
							}

							if (log.isTraceEnabled())
							{
								log.trace("Finished with job: '{}'", code);
							}

							// Sleep until the period is over (or not at all if it's
							// already passed)
							Instant nextExecution = startOfPeriod.plus(frequency);
							
							Duration timeToNextExecution = Duration.between(Instant.now(), nextExecution);

							if (!timeToNextExecution.isNegative())
							{
								try {
									Thread.sleep(timeToNextExecution.toMillis());
								}
								catch (InterruptedException e) {
									Thread.currentThread().interrupt();
								}
							}
						}
						log.trace("Task '{}' stopped", name);
					}
					catch (Exception x)
					{
						log.error("Task '{}' terminated", name, x);
					}
					finally
					{
						isStarted = false;
					}
				}
			};

			// Start the thread
			thread = new Thread(runnable, name + " Task");
			thread.setDaemon(isDaemon);
			thread.start();

			// We're started all right!
			isStarted = true;
		}
		else
		{
			throw new IllegalStateException("Attempt to start task that has already been started");
		}
	}