Threads Java: problems

JAX82JAX82 Juan PedroOrganization: PersonalProject: PersonalMember
Hi,

I'm using threads from a Java client. The client main function creates threads and each one calls remote methods in a Java server.

Each thread calls a very simple remote method (one of them writes a string like "hello world" and the otherone runs an application like "gedit" or "notepad.exe").

If I dont use threads it works well, but if I use them, I get allways the next error when I run the client:
Ice.CommunicatorDestroyedException
	at IceInternal.Instance.objectAdapterFactory(Instance.java:122)
	at Ice.ObjectPrxHelperBase.__getDelegate(ObjectPrxHelperBase.java:1038)
	at Demo.ExecutePrxHelper.executeApp(ExecutePrxHelper.java:42)
	at Demo.ExecutePrxHelper.executeApp(ExecutePrxHelper.java:19)
	at client.ServiceCaller.call(ServiceCaller.java:36)
	at client.ServiceCaller.run(ServiceCaller.java:49)
	at java.lang.Thread.run(Thread.java:595)

My client implementation is the next:
try {
			ic = Ice.Util.initialize(args);
			// ------------------------------------------------------------
			// CLIENT IMPLEMENTATION
			Ice.ObjectPrx basePrinter = ic
			.stringToProxy("Printer:default -p 10000");
			Ice.ObjectPrx baseExecute = ic
			.stringToProxy("Execute:default -p 10000");			
			Demo.PrinterPrx printer = Demo.PrinterPrxHelper.checkedCast(basePrinter);
			if (printer == null)
				throw new Error("Invalid Printer proxy");
			Demo.ExecutePrx execute = Demo.ExecutePrxHelper.checkedCast(baseExecute);
			if (execute == null)
				throw new Error("Invalid Execute proxy");
	
			new Thread(new ServiceCaller("Printer", "Hello world!!", printer)).start();
		        new Thread(new ServiceCaller("Execute", "gedit", execute)).start();	
			// ------------------------------------------------------------	
		} catch (Ice.LocalException e) {

The ServiceCaller runnable class:
public class ServiceCaller implements Runnable {

	private String serviceName;
	private String name;
	private Ice.ObjectPrx obj;

	public ServiceCaller(String serviceName, String name, Ice.ObjectPrx obj) {
		this.serviceName = serviceName;
		this.name = name;
		this.obj = obj;
	}

	public void call() {
		try {	
			if (serviceName.equals("Printer")) {
				Demo.PrinterPrx printer = (Demo.PrinterPrx) obj;
				printer.printString(name);				
                        }else if (serviceName.equals("Execute")) {
				Demo.ExecutePrx execute = (Demo.ExecutePrx) obj;
				execute.executeApp(name);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void run() {
		this.call();
	}

}

Thanks in advance.

Comments

  • mesmes CaliforniaMark SpruiellOrganization: ZeroC, Inc.Project: Ice DeveloperAdministrators, ZeroC Staff ZeroC Staff
    Hi,

    As the exception indicates, a program is attempting to use Ice after the communicator is destroyed. This is a common issue in multithreaded applications, and you can correct it by joining with each of your threads that uses Ice before destroying the communicator. If you're still having trouble, please provide a small, self-contained example that demonstrates the problem and we'll take a look at it.

    Take care,
    - Mark
  • JAX82JAX82 Juan PedroOrganization: PersonalProject: PersonalMember
    Thanks Mark, it works.
Sign In or Register to comment.