Results 1 to 6 of 6

Thread: Control thread speed in java

  1. #1
    Join Date
    Dec 2009
    Posts
    296

    Control thread speed in java

    Hello,
    I've created a program in which an object moves. I wanted to make sure we can see its traces. So I used a Vector and the thread of my program I wrote the code that allows the insertion of x and y position in the Vector. This means that each turn of thread a point is added / drawn. This makes the program go slower than that in advance. I would like to add my points in Vector may be the (speed of thread / 5) How can I control this? Thank you in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Control thread speed in java

    Hello,
    The first idea is that instead of Vector, you can use an ArrayList, which is more modern. It works exactly the same and has exactly the same role, his access is a bit faster. However, there was probably something else. How much is he objects registered? You are doing something special with your Vector? Am i correct? I think you should continue with the current code, I think this is correct.

  3. #3
    Join Date
    Dec 2009
    Posts
    211

    Re: Control thread speed in java

    Hello,
    I was also trying to do a similar kind of a program. In my program the time to draw the points recorded in the Vector that was choppy, that's why I have thought that if I put less points in it. The idea is to include less frequently coordinates of the object for more fluidity. I think my problem and the query posted are same. Both have a similar kind of a problem , so, if you have any idea about this then please do post back.

  4. #4
    Join Date
    Nov 2009
    Posts
    359

    Re: Control thread speed in java

    Hello,
    The Vector class are synchronized, it is slowed by the use of Thread. If the object in which you store your contact information is unique per thread, so is not shared between threads, so I advise you to use an ArrayList. But in all cases, except if you have a lot of coordinated, normally a Vector is still quite powerful. Perhaps the problem lies more in the display.

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Control thread speed in java

    Hello,
    If yes, I think it's because you assume that each loop always executes in milliseconds x, with x constant (you're basically synchronous). This is not true at all (it depends on the scheduling of threads, cpu load, etc. ...). In general, to do this, you must store the start time (start = System.currentTimeMillis ()), and then the new position is a function of time (System.currentTimeMillis () - start). For example, a linear function of time, position (x, y) could be ((current - start) / 200 (current - start) / 50) ... (to increase a pixel x every 5 ms (1 / 200) and a pixel in there every 20 ms (1 / 50).

  6. #6
    Join Date
    Dec 2009
    Posts
    296

    Re: Control thread speed in java

    Hello,
    Thanks for your replies, I am satisfied with your answers. Infact, it is only when I display the traces contained in the vector that starts rowing . And as I redraw each turn of thread the image of my object (which will be moved), I also redraws each round all the points that are in my Vector. It's probably the view. I would like to try so more examples on this and if I have any problem regarding those I will post back here.

Similar Threads

  1. Synchronize a producer thread and a consumer thread in C#
    By Ayuka in forum Software Development
    Replies: 6
    Last Post: 10-11-2010, 04:04 AM
  2. How to control thread priority in Windows Phone 7
    By Vengence in forum Portable Devices
    Replies: 5
    Last Post: 27-10-2010, 04:44 AM
  3. What is the Event Dispatch Thread in Java?
    By Flaco in forum Software Development
    Replies: 5
    Last Post: 14-02-2010, 05:28 AM
  4. Thread scheduling In Java
    By samualres in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 06:50 PM
  5. Thread Priorities in java
    By Aakarshan.d in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 02:02 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,445,816.22688 seconds with 17 queries