Results 1 to 2 of 2
  1. #1
    DaCapitalist is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default [SOLVED]Thread not waiting for another with join()

    Hey!

    I'm new here. Hope you can help me.

    I have a watchlist-Class which is supposed to refresh its data periodically.

    This is done by a "WatchlistThread". But since the refresh()-method should not only be started by the constructor but also by other (UI) classes, I need to terminate the thread there to restart it after.

    When I call the refresh() method from my UI, I always get a ThreadState-Exception. Can you tell me what I've overseen?

    Thanks a lot!
    DaC

    Java Code:
    package de.uni_mannheim.informatik.swt.dsds.ishare.domain.model;
    
    import java.util.HashMap;
    
    /**
     * Manages a watchlist of shares
     *
     */
    @SuppressWarnings("serial")
    public class Watchlist extends AbstractTableModel {
    	
    	int refreshRate = 60000;
    	
    	String name;
    	String description;
    	HashMap<ShareObject, Delimiter> shares;
    	private boolean mute = false;
    	
    	//Thread refreshing the table model with data from webserver
    	Thread watchlistThread = new Thread()
    	{
    		public void run()
    		{
    			if(getShareList() != null) {
    				for(ShareObject s: getShareList()) {
    					if(!this.isInterrupted()) {
    						s.refreshDynamicData();
    						fireTableDataChanged();
    					}
    					else {
    						return;
    					}
    				}
    			}
    		}
    	};
    	
    	/**
    	 * Creates new watchlist
    	 * @param name
    	 * @param description
    	 */
    	public Watchlist(String name, String description) {
    		this.name = name;
    		this.description = description;
    		this.shares = new HashMap<ShareObject, Delimiter>();
    		
    		//Refresh data from webservice periodically
            final class UpdateTask extends TimerTask {
                public void run(){
                	refresh();
                }
            }
    		Timer refreshTimer = new Timer();
            refreshTimer.schedule(new UpdateTask(), 0, refreshRate);
    	}
    	
    	/**
    	 * Invokes the watchlists refresh thread once
    	 */
    	public void refresh() {
    		this.fireTableDataChanged();
    		
    		if(watchlistThread.isAlive()) {
    			watchlistThread.interrupt();
    			try {
    				watchlistThread.join();
    			} catch (InterruptedException e) {e.printStackTrace();}
    		}
    		System.out.println(watchlistThread.getState());
    	    watchlistThread.start();
    	}
    
    	/**
    	 * Getter for name
    	 * @return the name
    	 */
    	public String getName() {
    		return name;
    	}
    
    	/**
    	 * Setter for name
    	 * @param name the name to set
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	/**
    	 * Getter for description
    	 * @return the description
    	 */
    	public String getDescription() {
    		return description;
    	}
    
    	/**
    	 * Setter for description
    	 * @param description the description to set
    	 */
    	public void setDescription(String description) {
    		this.description = description;
    	}
    
    	/**
    	 * Disable notifications from this watchlist
    	 * @param mute
    	 */
    	public void setMute(boolean mute) {
    		this.mute  = mute;
    	}
    	
    	/**
    	 * Disable notifications from this watchlist
    	 */
    	public boolean getMute() {
    		return this.mute;
    	}
    
    	/**
    	 * Adds (or overwrites) a share to the watchlist
    	 * @param share
    	 * @param limits
    	 * @return old share parameters if some already existed
    	 */
    	public Delimiter addShare(String symbol, String name, String index, Delimiter limits) {
    		Delimiter result;
    		if(shareExists(symbol)) {
    			result = shares.put(getShare(symbol), limits);
    		}
    		else {
    			result = shares.put(new ShareObject(symbol,name,index), limits);
    		}
    		//Update JTables
    		this.fireTableDataChanged();
    		return result;
    	}
    	
    	/**
    	 * Looks for a share in the watchlist with same symbol and removes it
    	 * @param share
    	 * @return old delimiter parameters or null if share not in watchlist
    	 */
    	public Delimiter removeShare(ShareObject share) {
    		Delimiter result = shares.remove(getShare(share.getSymbol()));;
    		//Update JTables
    		this.fireTableDataChanged();
    		return result;
    	}
    
    	/**
    	 * Empty watchlist so that user wil get no notifications anymore
    	 */
    	public void clear() {
    		shares.clear();
    		//Update JTables
    		this.fireTableDataChanged();
    	}
    
    	/**
    	 * Gets number of columns
    	 */
    	@Override
    	public int getColumnCount() {
    		return 5;
    	}
    
    	/**
    	 * Gets number of rows
    	 */
    	@Override
    	public int getRowCount() {
    		return shares.size();
    	}
    	
    	/**
    	 * Gets header text for a given table column
    	 */
    	public String getColumnName(int col) {
    		String value = "";
    		switch(col) {
    		case 0: value = Language.getText("SHARE_SYMBOL"); break;
    		case 1: value = Language.getText("SHARE_NAME"); break;
    		case 2: value = Language.getText("SHARE_INDEX"); break;
    		case 3: value = Language.getText("SHARE_VALUE"); break;
    		case 4: value = Language.getText("SHARE_DIFF"); break;
    		}
    		return value;
        }
    
    	/**
    	 * Gets value at given position
    	 * @param row
    	 * @param dltrAttribute
    	 */
    	@Override
    	public String getValueAt(int row, int dltrAttribute) {
    			String value = "";
    			switch(dltrAttribute) {
    			case 0: value = ((ShareObject)(shares.keySet().toArray()[row])).getSymbol(); break;
    			case 1: value = ((ShareObject)(shares.keySet().toArray()[row])).getName(); break;
    			case 2: value = ((ShareObject)(shares.keySet().toArray()[row])).getIndex(); break;
    			case 3: value = ((ShareObject)(shares.keySet().toArray()[row])).getPrice(); break;
    			case 4: value = ((ShareObject)(shares.keySet().toArray()[row])).getDiff(); break;
    			}
    			return value;
    	}
    
    	/**
    	 * Gets a synchronized view on all shares in this watchlist
    	 * @return Set<Share> shareList
    	 */
    	public Set<ShareObject> getShareList() {
    		if(shares.isEmpty()) { return null; }
    		else { return shares.keySet(); }
    	}
    	
    	/**
    	 * Gets the delimiter from a given share
    	 * @param ShareObject
    	 * @return Delimiter d
    	 */
    	public Delimiter getDelimiter(ShareObject key) {
    		ShareObject share = this.getShare(key.getSymbol());
    		return shares.get(share);
    	}
    	
    	/**
    	 * Checks wether a share with the given symbol is in the watchlist
    	 * @param symbol
    	 * @return true if share exists
    	 */
    	public boolean shareExists(String symbol) {
    		for ( int i  = 0; i<shares.size(); i++ ) {
    			if(((ShareObject) shares.keySet().toArray()[i]).getSymbol().equals(symbol)) {
    				return true;
    			}
    		}
    		return false;
    	}
    	
    	
    	/**
    	 * Gets a share via its symbol
    	 * @param symbol
    	 * @return the share if exits, null if not
    	 */
    	public ShareObject getShare(String symbol) {
    		for ( int i  = 0; i<shares.size(); i++ ) {
    			ShareObject current = ((ShareObject) shares.keySet().toArray()[i]);
    			if(current.getSymbol().equals(symbol)) {
    				return current;
    			}
    		}
    		return null;
    	}
    		
    }
    Last edited by DaCapitalist; 04-25-2011 at 07:03 PM.

  2. #2
    DaCapitalist is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    I solved it by myself looking in other communities where I found the hint, that java does not allow the reuse of an old thread object. I now create a new one within the refresh() every time it is called :)

Similar Threads

  1. Using Thread waiting() method
    By nicoeschpiko in forum New To Java
    Replies: 7
    Last Post: 12-11-2010, 09:24 PM
  2. Using Thread waiting() method
    By nicoeschpiko in forum Advanced Java
    Replies: 1
    Last Post: 12-11-2010, 04:50 PM
  3. Thread RUNNABLE or WAITING
    By Pushkar in forum Threads and Synchronization
    Replies: 10
    Last Post: 01-14-2010, 02:36 AM
  4. Join, basic question about thread
    By simorgh in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-13-2010, 01:01 AM
  5. Once again: waiting in a thread loop.
    By willemjav in forum Threads and Synchronization
    Replies: 115
    Last Post: 09-22-2008, 02:35 PM

Tags for this Thread

Posting Permissions

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