Quantcast
Channel: Tips – JRebirth's Blog
Viewing all articles
Browse latest Browse all 3

Tip #3: Never Write ‘new Thread()’ Again

$
0
0

As you probably know JRebirth has a powerful thread engine that kick away all coding complexity that could lead to User Interface corruptions, lags or the worst one a totally frozen interface.

It’s really difficult to create dynamically a lot of threads without adding some bugs to the application. JRebirth provides a powerful way to simplify your developments.

With the advent of Java 8 (with Method References) and JRebirth 8,  it’s now possible to chain and combine Tasks running into specific Thread in a very convenient way.

RefCommand

Call a single method into the wished thread
Let’s see an example:

It’s as simple as create a Ref object and calling the RefCommand from any Component.

final Ref ref = Ref.single()
		.priority(RunnablePriority.Highest)
		.runInto(RunType.JTP)
		.run(this::sayHello);
callCommand(RefCommand.class, ref);

...

public void sayHello() {
    System.out.println("hello");
}

Same thing but using a wave (allow to attach some WaveData and process them with a WaveListener)

final Ref ref = Ref.single()
		.priority(RunnablePriority.Highest)
		.runInto(RunType.JTP)
		.runWave(this::sayHelloWave);
callCommand(RefCommand.class, ref);

...

public void sayHelloWave(final Wave wave) {
    System.out.println("hello " + wave.getWUID());
}

GroupCommand

Obviously JRebirth Thread Engine allow to combine several tasks running in different threads.
Tasks can be run in parallel or sequentially.

In this example the MultiCommand will be run into JTP and its two children too.

final Ref ref = Ref.group()
		.sequential(true)
		.runInto(RunType.JTP)
		.priority(RunnablePriority.Highest)
		.add(
				Ref.single()
				.priority(RunnablePriority.Highest)
				.runInto(RunType.JTP)
				.run(this::sayHello),
				Ref.single()
				.priority(RunnablePriority.Highest)
				.runInto(RunType.JTP)
				.runWave(this::sayHelloWave)
				);

callCommand(GroupRefCommand.class, ref);

By building an object tree with GroupRef, SingleRef and RealRef objects it’s possible to run complex task by controlling their executions order, their priority and thread used.

final Ref ref = Ref.group()
		.sequential(true)
		.runInto(RunType.JTP)
		.priority(RunnablePriority.Highest)
		.add(
				Ref.real()
				.commandKey(Key.create(UpdateCursorCommand.class, Cursor.WAIT)),

				Ref.single()
				.priority(RunnablePriority.Highest)
				.runInto(RunType.JTP)
				.run(this::sayHello),

				Ref.group()
				.sequential(false) // Run commands in parallel
				.runInto(RunType.JTP)
				.priority(RunnablePriority.Highest)
				.add(
						Ref.single()
						.priority(RunnablePriority.Highest)
						.runInto(RunType.JIT)
						.run(this::doIntoJIT),

						Ref.single()
						.runInto(RunType.JAT) // No priority for JAT
						.runWave(this::doIntoJAT)
				),

				Ref.single()
				.priority(RunnablePriority.Highest)
				.runInto(RunType.JTP)
				.runWave(this::sayHelloWave),
						
				Ref.real()
				.commandKey(Key.create(UpdateCursorCommand.class, Cursor.DEFAULT))
													 
				);
callCommand(GroupRefCommand.class, ref);

Please note that the RealRef allow to call an existing command.

I hope you will enjoy this new feature !


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images