Problem with synchronization of IJ commands on a plugin

imagej

#1

Hello all,
I’m new here and I’m a Brazilian student, then sorry for some English errors.
I’m trying to create a synchronization on my plugin.
First I need to run Analyze Particles and then, after the Analyze Particles finish, execute another code.
However my another code finish before the Analyze Particles.
I’m executing the Analyze Particles with the “run” command, that for me would to stop the program execution until finalize.
How it’s not worked, I’m trying using Thread.
However the thread also not working.
The Analyze Particle thread and the other are locking.
Please I would to know why the run command is not stopping and why the threads are locking.
Thanks to all.
The code is below.

public void applyAnalyzeParticles() {
		Thread thread1 = new Thread(new Runnable() {  
			public void run() {  
				IJ.run("Analyze Particles...");
			}
		});
		thread1.setPriority(Thread.NORM_PRIORITY);
		thread1.start();
		try {
			thread1.join();
		}
		catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
                }
		
        Thread thread2 = new Thread(new Runnable() {  
        	public void run() {  
            	while (WindowManager.getWindowCount() < 2) {
            	}
            	IJ.showMessage(Integer.toString(WindowManager.getWindowCount()));
                }
		});
		thread2.setPriority(Thread.NORM_PRIORITY);
		thread2.start();
		try {
			thread2.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

#2

IJ.run calls the Executor class which already implements the Runnable interface. So the commands are already executed in a seperate thread which leads to this multi threaded behaviour.

I think one workaround to get your required behaviour (in sequence) would be to use the runMacro command which hopefully returns when the job is finished:

IJ.runMacro("run(\"Analyze Particles...\")");
System.out.println("Finished Particle Analysis!");

Here the same command with arguments to avoid dialogs:

IJ.runMacro("run(\"Analyze Particles...\", \"size=0-Infinity circularity=0.00-1.00 show=Nothing display clear stack\")");

#3

Exactly what I needed. I tested it and run very well.
Thank you very much for your help!
Regards
Duncan.