Results 1 to 9 of 9
  1. #1
    kuki is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Post Serializing a tree map every one minute

    Java Code:
    public class StoreMessage extends Thread {
    private static int timeToRun = 60000; // 1 minute
    public static Map <Long,String[]> map1=new TreeMap<Long,String[]>()
    public static void main(String args[])throws Exception{
    
    long runUntilMillis = System.currentTimeMillis() + timeToRun;
    		while (true) {
    		    long now = System.currentTimeMillis();
    		    if (now >= runUntilMillis) {
    		       break;
    		    }
    		    long precise_time=TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis())+(System.nanoTime()-start_nanotime);
    		    map1.put(precise_time, args);
                   }
               returning();
    	 
    	}
    	
    	public static byte[] returning()throws Exception
    	{
    		return serializer.serialize(map1);
    		
    	}
    	
    }
    Java Code:
    public class serializer {
    	//serializes an object and returns a byte array
    	public static byte[] serialize(Object map) throws IOException 
    	  {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            ObjectOutputStream o = new ObjectOutputStream(b);
            o.writeObject(map);
            return b.toByteArray();
          }
    	
    	//de-serialization of the byte array and returns an object  
    	public static Object toObject (byte[] bytes)
    	{
    	  Object obj = null;
    	  try 
    	   {
    	    ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
    	    ObjectInputStream ois = new ObjectInputStream (bis);
    	    obj = ois.readObject();
    	   }
    	  catch (Exception ex) { }
    	  return obj;
        }
    }
    I have a class Message and its object message which are passed to the class store message. The "message" objects are string objects passed as command-line arguments. For example "hi" "hello" "good day" -- these are 3 command-line arguments which as passed to this class as Message objects. In this StoreMessage class, i expect it to store all the messages it receives in one minute to a tree map and after a minute has passed, create a new tree map and serialize the previous map and return it to another class which calls for the byte[].
    The problem faced here is, the calling class is not receiving the byte[]. Also when i tried printing the map using SOP statement after the while loop, it does not print it and inside the loop when i did the same, i noticed, in my map each of the message is stored more than 5-6 times. Why is the loop spinning this way?Please help
    Last edited by kuki; 04-05-2012 at 05:58 AM.

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

    Default Re: Serializing a tree map every one minute

    Can you make a small simple program that compile, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kuki is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Serializing a tree map every one minute

    I didn't get you Sir..

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Serializing a tree map every one minute

    Create a program that compiles, executes and shows the problem. Post the code for that program here so we can copy it to our computers, compile it, execute it and see the problem.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    kuki is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Serializing a tree map every one minute

    Sir, this is that code...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Serializing a tree map every one minute

    this is that code...
    Where have you posted code that will compile and execute and show the problem?
    For example where is the main() method?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    kuki is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Serializing a tree map every one minute

    Done editing Sir..

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Serializing a tree map every one minute

    Don't edit your posts after there are responses, except maybe to correct a spelling mistake, as that makes responses lose their relevance and makes the thread difficult to follow.

    Post a new response instead.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Serializing a tree map every one minute

    The code you have posted does not compile. I get 17 errors.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Please Help Me!!! Last minute test questions...
    By justinm231 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 06:42 PM
  2. Replies: 10
    Last Post: 03-29-2011, 11:09 AM
  3. Replies: 4
    Last Post: 05-26-2010, 08:30 PM
  4. Replies: 0
    Last Post: 04-04-2010, 07:40 AM
  5. Serializing
    By shashanksworld in forum New To Java
    Replies: 7
    Last Post: 03-29-2010, 04:47 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
  •