public abstract class Event implements Runnable {
public void run() {
try {
Thread.sleep(delayTime);
action();
} catch(Exception e) {e.printStackTrace();}
}
}
I have this event class above, when I try to start the thread it runs the first command of the thread - Thread.sleep(delayTime); Since the Event class is abstract I want to run some of its child class methods. For example, when I call action(); it should run the action method from the below child class
public class ThermostatNight extends Event {
public ThermostatNight(long delayTime) {
super(delayTime);
}
public void action() {
System.out.println(this);
thermostat = "Night";
}
public String toString() {return "Thermostat on night setting";}
}
There are many such child classes, like ThermostatDay, FanOn, FanOff who are very similar as above. What should I do the call action(); after sleep is called from the run() command in Event class ?
Any ideas?
Your help is appreciated!
Create and instance variable of type Event and a constructor that takes in an
Event as parameter
public class Event implements Serializable, Runnable {
private Event childClass;
public Event(long delayTime, Event childClass) {
this.delayTime = delayTime;
this.childClass = childClass;
}
public void run() {
try {
Thread.sleep(delayTime);
childClass.action();
} catch(Exception e) {e.printStackTrace();}
}
}
I realized the issue here.
You can see action() is called in try {} below:
public void run() {
try {
Thread.sleep(delayTime);
action();
} catch(Exception e) {e.printStackTrace();}
}
If you push it out like below, the code should work fine.
public void run() {
try {
Thread.sleep(delayTime);
} catch(Exception e) {e.printStackTrace();}
action();
}
Related
Suppose you got a JTree with a model that implements TreeModel, and all nodes implement TreeNode.
Suppose then that something happens in the background with the model (not through the GUI) like a CRUD-event, that update the model and should update the JTree.
Since the model is CRUD-affected from other views it does not seems like a good idea to use the DefaultTreeModel for this task, correct me if I'm wrong.
I guess you need to signal the change to the TreeModel in somehow, like fire some event in some way?
Btw I have not managed to implement the methods:
public void addTreeModelListener( TreeModelListener l )
public void removeTreeModelListener( TreeModelListener l )
I guess these methods need to be implemented for such a feature.
I like to use this kind of generic ListenerList:
public class ListenerList {
private final List<Object> list = new ArrayList<Object>();
public ListenerList() {
}
public void addListener(Object listener) {
list.add(listener);
}
public void removeListener(Object listener) {
list.remove(listener);
}
public <T> T getNotifier(Class<T> intf) {
ClassLoader cl = intf.getClassLoader();
return intf.cast(Proxy.newProxyInstance(cl, new Class[] {intf},
(Object proxy, Method method, Object[] args)
-> actualInvoke(method, args)));
}
private Object actualInvoke(Method method, Object args[]) {
Object result = null;
for (Object listener: list) {
try {
result = method.invoke(listener, args);
} catch (IllegalAccessException e) {
LOG.error("Error invoking listener method", e);
} catch (InvocationTargetException e) {
LOG.error("Error invoking listener method", e);
}
}
return result;
}
}
That I use in my model class:
public class MyTreeModel implements TreeModel {
private final ListenerList listeners = new ListenerList();
private final TreeModelListener notifier = listeners.getNotifier(TreeModelListener.class);
public void addTreeModelListener( TreeModelListener l ) {
listeners.addListener(l);
}
public void removeTreeModelListener( TreeModelListener l ) {
listeners.removeListener(l);
}
protected void fireTreeNodesChanged(TreeModelEvent e) {
notifier.treeNodesChanged(e);
}
protected void fireTreeNodesInserted(TreeModelEvent e) {
notifier.treeNodesInserted(e);
}
protected void fireTreeNodesRemoved(TreeModelEvent e) {
notifier.treeNodesRemoved(e);
}
protected void fireTreeStructureChanged(TreeModelEvent e)
notifier.treeStructureChanged(e);
}
...
}
In an interview I was asked to come up with an approach which will ensure that while thread T1 and T3 can access a method of a class, T2 cannot access the method.
I am unable to provide any solution to this. Could you please provide an example with an explanation?
I have later come up with the following solution. Is it efficient?
package JavaProgramming;
public class EligibleThread implements Runnable {
public void method1() {
System.out.println("Hello");
}
public static void main(String[] args) {
EligibleThread t1 = new EligibleThread();
EligibleThread t2 = new EligibleThread();
Thread t11 = new Thread(t1, "t1");
Thread t22 = new Thread(t2, "t2");
t11.start();
t22.start();
}
public void run() {
if (Thread.currentThread().getName() != "t2") {
method1();
} else{
try {
throw new Exception("Access is denied");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
You can use protected modifier, like following code. T1 can call aMethod() by extending Main class, but T2 can't call aMethod().
public class Main {
protected void aMethod() {
}
}
class T1 extends Main implements Runnable{
#Override
public void run() {
aMethod();
}
}
class T2 implements Runnable{
#Override
public void run() {
// here can't call Main.aMethod()
}
}
I have an abstract superclass AsyncActor which extends the Actor class. This superclass contains two abstract methods, notifySuccess and notifyFailure. notifyFailure is overriden in AsyncActor, but notifySuccess isn't because it's implementation changes from subclass to subclass. I have two actors, CreateObjActor and DeleteObjActor. Each extends AsyncActor and overrides notifySuccess. The execute method located in AsyncTask calls notifySuccess, but nothing happens.
public class AsyncActor extends Action<String> {
private Future future;
private final Action action;
private final ExecutorService executor = Executors.newSingleThreadExecutor();
public AsyncActor(Action action) {
this.action = action;
}
public Action getAction() {
return action;
}
public Future getFuture() {
return future;
}
public void execute() {
if(action.execute()) {
executeAsynchronously();
}
}
public void executeAsynchronously() {
ServiceFw.log.debug("Writing directory to file...");
Callable<String> asyncTask = () -> {
try {
ServiceFw.entryManager.writeBack();
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
ServiceFw.log.debug("Exception thrown during asynchronous wait");
e.printStackTrace();
} catch (BusinessException e) {
notifyFailure();
}
return "write back operation";
};
future = executor.submit(asyncTask);
Runnable poll = () -> {
if(future!=null) {
notifySuccess();
} else {
error.setSeverity(ExtendedError.Severity.Warning);
notifyFailure(error);
}
};
poll.run();
}
#Override
public void notifySuccess() {
}
#Override
public void notifyFailure() {
ServiceFw.log.error("Error with asynchronous processing of write back" );
ServiceFw.entryManager.deleteActor(this);
}
}
public class CreateObjActor extends AsyncActor {
public CreateObjActor(Action action) {
super(action);
}
#Override
public void notifySuccess() {
try {
ServiceFw.log.debug("Finished asynchronous operation: " + getFuture().get());
Entry entry = getAction().getEntryModel();
if (getAction().isNotify()) {
Notification notification = new Notification(entry);
ServiceFw.notificationDispatcher.dispatchNotification(notification);
try {
ServiceFw.eventFramework.dispatchEvent("entry-added");
} catch (BusinessException e) {
ServiceFw.log.error("Event could not be dispatched for newly added entry: " + entry.toString());
}
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
ServiceFw.entryManager.deleteActor(this);
}
}
}
I've tried making AsyncActor not abstract and providing an empty implementation of notifySuccess, but this didn't work either. Is there a way I can call the method from the superclass? The execute method contains a lot of code (not shown here) that would result in a lot of duplicate code if it had to be placed in each subclass.
I am trying to write a Utility class that helps to execute a task on a Separate Thread, providing the ability to do something before the task starts, and something after the task ends.
Something similar to android's AsyncTask
Here is such a class.
class MySync
{
public void preExecute() {}
public void executeInBackground() {}
public void postExecute() {}
public final void execute()
{
threadExecute.start();
}
private final Thread threadExecute = new Thread()
{
#Override
public void run()
{
try
{
MySync.this.preExecute();
MySync.this.executeInBackground();
MySync.this.postExecute();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
}
Here is how this class is supposed to be used. Consumer of the class will override the methods as per the requirement.
class RegisterStudent extends MySync
{
#Override
public void preExecute()
{
System.out.println("Validating Student details. Please wait...");
try
{
Thread.sleep(2000);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
#Override
public void executeInBackground()
{
System.out.println("Storing student details into Database on Server. Please wait...");
try
{
Thread.sleep(4000);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
#Override
public void postExecute()
{
System.out.println("Student details saved Successfully.");
}
}
And finally starting the task:
public class AsyncDemo
{
public static void main(String... args)
{
new RegisterStudent().execute();
}
}
It seems to work fine. My question is, is this the correct way of achieving the Objective as mentioned in the Title? Any suggestions on how best this can be implemented?
What I don't like with your approach is the fact that you create a new thread each time you create a new instance of MySync which is not scalable if you intend to create a lot of instances of your Object moreover it is costly to create a Thread, if I were you I would use an executor in order to limit the total amount of threads allocated to execute your tasks asynchronously, here is how you can do it if you want to use only one thread:
ExecutorService executor = Executors.newFixedThreadPool(1);
I would also re-write your code for something like this:
public abstract class MySync implements Runnable {
#Override
public final void run() {
try {
preExecute();
executeInBackground();
} finally {
postExecute();
}
}
protected abstract void preExecute();
protected abstract void executeInBackground();
protected abstract void postExecute();
}
This way you define the whole logic for all the implementations.
Then you can submit your task like this:
executor.submit(new RegisterStudent());
What's bad about this is that you're forcing users to extend your class. In java you can only extend 1 class. So a framework should not take that away.
Rather use an interface:
public interface AsyncTask {
public default void preExecute() {}
public default void executeInBackground() {}
public default void postExecute() {}
}
And have users pass that to your utility class:
class MySync
{
private AsyncTask task;
public MySync(AsyncTask task) {
this.task = task;
}
public final void execute()
{
threadExecute.start();
}
private final Thread threadExecute = new Thread()
{
#Override
public void run()
{
try
{
MySync.this.task.preExecute();
MySync.this.task.executeInBackground();
MySync.this.task.postExecute();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
};
}
Loader is exactly what you are looking for.
Here is introduction for loader
https://developer.android.com/guide/components/loaders.html
https://developer.android.com/reference/android/content/Loader.html
I'm having a trouble with Runnable and Thread implementations. I have this abstract class, that can not be modified:
abstract class Ordenador {
...
protected Ordenador(String nombre, int[] array) {
...
}
protected void escribir() {
...
}
protected abstract void ordenar();
}
And this sort algorithm that inherit from the class above and implements the run() method, which call the sorting one.
class Burbuja extends Ordenador implements Runnable {
protected Burbuja(String nombre, int[] array) {
super(nombre, array);
}
protected void ordenar() {
....
}
public void esperar() {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
public void run() {
this.ordenar();
}
}
Finally I have my main class that creates a random array and create a new Burbuja object that sort the array. The problem is that when calling b.join() the array stay the same so de ordenar() method doesn't get called.
class Aplicacion {
public static void main(String[] args) {
...
Burbuja burbuja = new Burbuja("Burbuja", array);
Thread b = new Thread(burbuja);
...
try {
b.join();
s.join();
... more sorting algorithms...
} catch (Exception ex) {
ex.printStackTrace();
System.exit(-1);
}
System.out.println("");
burbuja.escribir();
}
}
I tried modificating some parts of the code but doesn't work neither.
You have to call the start() method on your thread object
https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
Your Thread b = new Thread(burbuja); is right, but you forget to call the start method, b.start();