开发者

Java threads query

开发者 https://www.devze.com 2022-12-17 13:41 出处:网络
Im working on a java application that involves threads. So i just wrote a piece of code to just familiarize myself with the execution of multiple yet concurrent threads

Im working on a java application that involves threads. So i just wrote a piece of code to just familiarize myself with the execution of multiple yet concurrent threads

public class thready implements Runnable{
private int num;

public thready(int a) {
    this.num=a;
}
public void run() {
  System.out.println("This is thread num"+num);
  for (int i=num;i<100;i++)
  {
      System.out.println(i);
  }
}
public static void main(String [] args)
{
    Runnable runnable =new thready(1);
    Runnable run= new thready(2);
    Thread t1=new Thread(runnable);
    Thread t2=new Thread(run);
    t1.start();
    t2.start();
}}

Now from the output of this code, I think at any point in time only 1 thread is executing and the execution seems to alternate between the threads. Now i would like to know if my understanding of the situation is correct. And if it is I would like to know if there is any way in which i could get both threads to executing simultaneously as i wish to incorporate this scenario in a situation wherein i want to write a tcp/ip socket listener that simultaneousl开发者_C百科y listens on 2 ports, at the same time. And such a scenario cant have any downtime. Any suggestions/advice would be of great help.

Cheers


How many processors does your machine have? If you have multiple cores, then both threads should be running at the same time. However, console output may well be buffered and will require locking internally - that's likely to be the effect you're seeing.

The easiest way to test this is to make the threads do some real work, and time them. First run the two tasks sequentially, then run them in parallel on two different threads. If the two tasks don't interact with each other at all (including "hidden" interactions like the console) then you should see a roughly 2x performance improvement using two threads - if you have two cores or more.

As Thilo said though, this may well not be relevant for your real scenario anyway. Even a single-threaded system can still listen on two sockets, although it's easier to have one thread responsible for each socket. In most situations where you're listening on sockets, you'll spend a lot of the time waiting for more data anyway - in which case it doesn't matter whether you've got more than one core or not.

EDIT: As you're running on a machine with a single core (and assuming no hyperthreading) you will only get one thread executing at a time, pretty much by definition. The scheduler will make sure that both threads get CPU time, but they'll basically have to take turns.


If you have more than one CPU, both threads can run simultaneously. Even if you have only one CPU, as soon as one of the threads waits for I/O, the other can use the CPU. The JVM will most likely also try to dice out CPU time slices fairly. So for all practical purposes (unless all they do is use the CPU), your threads will run simultaneously (as in: within a given second, each of them had access to the CPU).

So even with a single CPU, you can have two threads listening on a TCP/IP socket each.


Make the threads sleep in between the println statements. What you have executes too fast for you to see the effect.


Threads are just a method of virtualizing the CPU so that it can be used by several applications/threads simultaneously. But as the CPU can only execute one program at a time, the Operating System switches between the different threads/processes very fast.

If you have a CPU with just one core (leaving aside hyperthreading) then your observation, that only one thread is executing at a time, is completely correct. And it's not possible in any other way, you're not doing anything wrong.


If the threads each take less than a single CPU quantum, they will appear to run sequentially. Printing 100 numbers in a row is probably not intensive enough to use up an entire quantum, so you're probably seeing sequential running of threads.

As well, like others have suggested, you probably have two CPU, or a hyperthreaded CPU at least. The last pure single core systems were produced around a decade ago, so it's unlikely that your threads aren't running side-by-side.

Try increasing the amount of processing that you do, and you might see the output intermingle. Be aware that when you do, System.out.println is NOT threadsafe, as far as I know. You'll get one thread interrupting the output of another mid-line.


They do run simultaneously, they just can't use the outputstream at the same time.

Replace your run- method with this:

public void run() {
   for (int i=num;i<100;i++) {
      try {
         Thread.sleep(100);
         System.out.println("Thread " + num + ": " + i);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }  
   }
}


If you are getting many messages per second and processing each piece of data takes few milliseconds to few seconds, it is not a good idea to start one-thread per message. Ultimately number of threads spawned are limited by the underlying OS. You may get out-of-threads error or something like that.

Java 5 introduced Thread Pool framework where you can allocate a fixed number of threads and submit the job (instance of Runnable). This framework will run the job in one of the available thread in the pool. It is more efficient as there is not much context switching done. I wrote a blog entry to jump-start on this framework.

http://dudefrommangalore.blogspot.com/2010/01/concurrency-in-java.html

Cheers, -- baliga


For the question on listening on 2 ports, clients has to send message to one of them. But since both ports are opened to accept connections within a single JVM, if the JVM fails having 2 ports does not provide you high-availability.

Usual pattern for writing a server which listen on a port is to have one thread listen on the port. As soon as the data arrives, spawn another thread, hand-over the content as well as the client socket to the newly spawned thread and continue accepting new messages.

Another pattern is to have multiple threads listen on the same socket. When client connects, connection is made to one of the thread.


Two ways this could go wrong:

  • System.out.println() may use a buffer, you should call flush() to get it to the screen.
  • There has to be some synchronisation build into the System.out object or you couldn't use it in a multithreaded application without messing up the output, so it is likely that one thread holds a lock for most of the time, making the other thread wait. Try using System.out in one thread and Sytem.err in the other.


Go and read up on multitasking and multiprogramming. http://en.wikipedia.org/wiki/Computer_multitasking

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号