Results 1 to 4 of 4
  1. #1
    Joshy910 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default Synchronisation problem

    I have two classes called ClassView and Chatter. The Chatter class has an instance of ClassView. Within Chatter a method called getIMessage is called. This method resides in ClassView and does some UI operations.

    The problem is when the getIMessage method is called from outside the UI part of ClassView it doesn't run. It runs fine when called from within the UI part of ClassView, but if it is called from anywhere else nothing seems to happen.
    No exception is thrown.

    The fact that it runs fine when called from the UI part makes me think it is some kind of thread synchronisation error. I have read that this can be fixed using the (a)syncExec methods but I am sure exactly of how to use these.

    When I run the debugger on the code it says something like ThreadPoolError. I am not sure what this means as I have only started using SWT.

    Here is the code for Chatter:


    Java Code:
    package instantmessengerplugin;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.TableItem;
    import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.ChatManager;
    import org.jivesoftware.smack.MessageListener;
    import org.jivesoftware.smack.PacketListener;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.filter.MessageTypeFilter;
    import org.jivesoftware.smack.filter.PacketFilter;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.packet.Packet;
    
    public class Chatter {
    	
    	public XMPPConnection connection;
    	public String user;
    	public ClassView classView;
    	public Chat chat;
    	public PacketFilter packetFilter;
    	public PacketListener listener;
    	
    	
    	
    	
    	public Chatter(XMPPConnection connection1,String user1, Display dist)
    	{
    		connection  = connection1;
    		user = user1;
    		
    		openChat();
    		classView = new ClassView(dist,chat);
    	}
    	
    	public void  openChat()
    	{
    		ChatManager cm = connection.getChatManager();
    		chat = cm.createChat(user, new MessageListener()
    		{
    			public void processMessage(Chat chat ,final Message message)
    			{
    				
    				classView.getIMessage(message.getBody());
    					
    					
    				
    						
    					
    				
    			}
    		}
    		
    		
    		
    		
    		);
    		
    		
    	}
    	
    	
    	
    	
    
    }
    and here is the code for ClassView:

    Java Code:
    package instantmessengerplugin;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Table;
    import org.eclipse.swt.widgets.TableItem;
    import org.eclipse.swt.widgets.Text;
    import org.jivesoftware.smack.Chat;
    import org.jivesoftware.smack.ChatManager;
    import org.jivesoftware.smack.MessageListener;
    import org.jivesoftware.smack.Roster;
    import org.jivesoftware.smack.XMPPConnection;
    import org.jivesoftware.smack.XMPPException;
    import org.jivesoftware.smack.packet.Message;
    import org.jivesoftware.smack.packet.Packet;
    
    
    public class ClassView {
    	
    	public Display displayChat;
    	public Shell shellChat;
    	public Table chatViewer;
    	public Chat chat;
    	public String iMessage;
    	
    	public ClassView(Display dist,Chat chat1){
    		
    		chat = chat1;
    		iMessage = "";
    		
    		displayChat = dist;
    		shellChat = new Shell(displayChat);
    		GridLayout gridLayout = new GridLayout();
    		gridLayout.numColumns = 2;
    		shellChat.setLayout(gridLayout);
    		
    		Label contact = new Label(shellChat,SWT.NONE);
    		contact.setText(chat.getParticipant());
    		GridData data = new GridData();
    		data.horizontalAlignment = GridData.FILL;
    		data.horizontalSpan = 2;
    		data.grabExcessHorizontalSpace = true;
    		data.grabExcessVerticalSpace = true;
    		
    		chatViewer = new Table(shellChat,SWT.NONE);
    		data = new GridData();
    		data.horizontalSpan = 2;
    		data.horizontalAlignment = GridData.FILL;
    		data.verticalAlignment = GridData.FILL;
    		data.grabExcessHorizontalSpace = true;
    		chatViewer.setLayoutData(data);
    		
    		final Text chatBox = new Text(shellChat,SWT.SINGLE);
    		data = new GridData();
    		data.verticalAlignment = GridData.FILL;
    		data.horizontalAlignment = GridData.FILL;
    		data.grabExcessHorizontalSpace = true;
    		chatBox.setLayoutData(data);
    		
    		Button send = new Button(shellChat,SWT.PUSH);
    		send.setText("Send");
    		
    		send.addSelectionListener(new SelectionAdapter(){
    	
    			public void widgetSelected(SelectionEvent e)
    			{
    				
    				
    				String s = chatBox.getText();
    				TableItem item = new TableItem(chatViewer, SWT.SINGLE);
    				item.setText("Me: " + s);
    				try {
    					chat.sendMessage(s);
    				} catch (Exception e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				
    				chatBox.setText("");
    			}
    			
    		});
    	
    		
    		
    		
    		
    		
    		
    		
    		
    		
    		shellChat.pack();
    		shellChat.open();
    		
    		while(!shellChat.isDisposed())
    		{
    			if(!displayChat.readAndDispatch())
    			{
    				displayChat.sleep();
    			}
    		}
    		
    		
    		
    	}
    	
    	public void getIMessage(String msg)
    	{
    		iMessage = msg;
    		TableItem item = new TableItem(chatViewer,SWT.NONE);
    		item.setText("Them: " + iMessage);
    		chatViewer.redraw();
    			
    		
    		
    	}
    	
    	
    	
    	public void updateChat()
    	{
    		
    				
    				
    			
    		
    			
    	}
    	
    	
    	
    }
    I have a feeling the problem has to with the code not being in sync with the UI thread but I thought that this would throw an exception. So I could be wrong.

    Does anyone know what the problem is and how I can fix it?

    Thanks
    Josh.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default

    it doesn't run.
    Have you tried debugging by adding some println() statements to show execution flow and how variables change?

    Since you have several packages not in the JDK, it could be hard for anyone to test your code.

  3. #3
    Joshy910 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    Yeah I am using a third party api. I realise that makes it difficult for people to test. Unfortunately it also means the debugger doesn't work so well because it doesn't have the source for the classes in those APIs.

    I tried what you suggested, I put in println() statements.

    I put one in the processMessage() method in Chatter before the getIMessage method call. The println() part works fine.

    I put one in the getIMessage method at the start of that. It doesn't print anything out so it seems like the method is not being called. I'll have a go with println() again and see what I can do

    I did think that i should do Display.getDefault().syncExec(runnable) and put some code in the runnable part but I am not sure which part of the code I should wrap with that. Of course this is only the solution if it's a UI Thread problem.

  4. #4
    Joshy910 is offline Member
    Join Date
    Aug 2010
    Posts
    4
    Rep Power
    0

    Default

    Here is the debug stack showing what is being called at execution of the

    classView.getIMessage(message.getBody()); call

    Java Code:
    FutureTask$Sync.innerRun() line: not available [local variables unavailable]	
    FutureTask<V>.run() line: not available	
    ThreadPoolExecutor$Worker.runTask(Runnable) line: not available	
    ThreadPoolExecutor$Worker.run() line: not available	
    Thread.run() line: not available [local variables unavailable]
    I am not sure what it means exactly, but the mention of Threads made me think that it was a problem with something not being synchronised with a Thread.

Similar Threads

  1. thread synchronisation
    By nabila.abdessaied in forum Threads and Synchronization
    Replies: 0
    Last Post: 04-01-2009, 05:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •