Java线程查询 [英] Java threads query

查看:93
本文介绍了Java线程查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理涉及线程的Java应用程序.所以我只写了一段代码,以使自己熟悉多个并发线程的执行

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();
}}

现在从此代码的输出中,我认为在任何时间点都只有1个线程正在执行,并且执行似乎在线程之间交替进行.现在,我想知道我对情况的理解是否正确.如果是这样,我想知道是否有任何一种方法可以让两个线程同时执行,因为我希望在要编写同时监听2的tcp/ip套接字监听器的情况下合并此方案.端口.而且这种情况下不能有任何停机时间. 任何建议/建议都会有很大帮助.

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 simultaneously listens on 2 ports, at the same time. And such a scenario cant have any downtime. Any suggestions/advice would be of great help.

欢呼

推荐答案

您的计算机有多少个处理器?如果您有多个核心,则两个线程应同时运行.但是,控制台输出可能会被缓冲,并且需要在内部进行锁定-这很可能是您所看到的效果.

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.

最简单的测试方法是使线程做一些实际的工作,并计时它们.首先依次运行两个任务,然后在两个不同的线程上并行运行它们.如果两个任务根本不交互(包括控制台之类的隐藏"交互),那么您应该看到使用两个线程的大致性能提高了2倍-如果您拥有两个或更多核心

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.

正如Thilo所说,无论如何这可能与您的实际情况无关.即使是单线程系统,仍然可以在两个套接字上侦听,尽管让一个线程负责每个套接字更容易.在大多数情况下,当您在侦听套接字时,无论如何您都会花费大量时间等待更多数据-在这种情况下,是否有多个内核都没有关系.

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.

当您在具有单核(并且假设没有超线程)的计算机上运行时, 一次只能执行一个线程,这在定义上是差不多的.调度程序将确保两个线程都获得CPU时间,但是基本上它们必须轮流使用.

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.

这篇关于Java线程查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆