首页 \ 问答 \ 检查Internet连接的可用性+两个线程之间的通信(Check availability of Internet Connection + Communicate between 2 threads)

检查Internet连接的可用性+两个线程之间的通信(Check availability of Internet Connection + Communicate between 2 threads)

我有两个问题:

  1. 如何检查Internet连接是打开还是关闭? 我正在使用Html Unit,我正在使用Windows。

  2. 我想制作一个JLabel,说明我的JFrame中的互联网连接的可用性。 就像是:

while(true)
{
    if(isOnline()) label.setText("online");
    else label.setText("offline");
}

但我认为我需要2个备份线程,但是我怎样才能创建这2个线程并在它们之间进行通信并实现这一目标?


I have 2 questions:

  1. How can I check if Internet connection is whether turned on or turned off? I'm using Html Unit and I'm working on Windows.

  2. I want to make a JLabel that states the availability of internet connection in my JFrame. Something like:

while(true)
{
    if(isOnline()) label.setText("online");
    else label.setText("offline");
}

but I think I need 2 sperated threads, but how could I create these 2 threads and communicate between them and achieve this?


原文:https://stackoverflow.com/questions/7183135
更新时间:2023-05-17 21:05

最满意答案

Processing附带的一个例子就是这样:使用动量守恒方程来计算多个球的速度。

这个例子在这里可用,或者您可以通过转到File - > Examples从Processing编辑器运行它。

该示例包含相当多的代码,但有趣的部分在这里:

void checkCollision(Ball other) {

    // get distances between the balls components
    PVector bVect = PVector.sub(other.position, position);

    // calculate magnitude of the vector separating the balls
    float bVectMag = bVect.mag();

    if (bVectMag < r + other.r) {
      // get angle of bVect
      float theta  = bVect.heading();
      // precalculate trig values
      float sine = sin(theta);
      float cosine = cos(theta);

      /* bTemp will hold rotated ball positions. You 
       just need to worry about bTemp[1] position*/
      PVector[] bTemp = {
        new PVector(), new PVector()
        };

        /* this ball's position is relative to the other
         so you can use the vector between them (bVect) as the 
         reference point in the rotation expressions.
         bTemp[0].position.x and bTemp[0].position.y will initialize
         automatically to 0.0, which is what you want
         since b[1] will rotate around b[0] */
        bTemp[1].x  = cosine * bVect.x + sine * bVect.y;
      bTemp[1].y  = cosine * bVect.y - sine * bVect.x;

      // rotate Temporary velocities
      PVector[] vTemp = {
        new PVector(), new PVector()
        };

        vTemp[0].x  = cosine * velocity.x + sine * velocity.y;
      vTemp[0].y  = cosine * velocity.y - sine * velocity.x;
      vTemp[1].x  = cosine * other.velocity.x + sine * other.velocity.y;
      vTemp[1].y  = cosine * other.velocity.y - sine * other.velocity.x;

      /* Now that velocities are rotated, you can use 1D
       conservation of momentum equations to calculate 
       the final velocity along the x-axis. */
      PVector[] vFinal = {  
        new PVector(), new PVector()
        };

      // final rotated velocity for b[0]
      vFinal[0].x = ((m - other.m) * vTemp[0].x + 2 * other.m * vTemp[1].x) / (m + other.m);
      vFinal[0].y = vTemp[0].y;

      // final rotated velocity for b[0]
      vFinal[1].x = ((other.m - m) * vTemp[1].x + 2 * m * vTemp[0].x) / (m + other.m);
      vFinal[1].y = vTemp[1].y;

      // hack to avoid clumping
      bTemp[0].x += vFinal[0].x;
      bTemp[1].x += vFinal[1].x;

      /* Rotate ball positions and velocities back
       Reverse signs in trig expressions to rotate 
       in the opposite direction */
      // rotate balls
      PVector[] bFinal = { 
        new PVector(), new PVector()
        };

      bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y;
      bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x;
      bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y;
      bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x;

      // update balls to screen position
      other.position.x = position.x + bFinal[1].x;
      other.position.y = position.y + bFinal[1].y;

      position.add(bFinal[0]);

      // update velocities
      velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y;
      velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x;
      other.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y;
      other.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x;
    }
  }

One of the examples that comes with Processing shows exactly this: using the conservation of momentum equations to calculate the velocity of multiple balls.

That example is available here, or you can just run it from the Processing editor by going to File -> Examples.

The example contains quite a bit of code, but the interesting part is here:

void checkCollision(Ball other) {

    // get distances between the balls components
    PVector bVect = PVector.sub(other.position, position);

    // calculate magnitude of the vector separating the balls
    float bVectMag = bVect.mag();

    if (bVectMag < r + other.r) {
      // get angle of bVect
      float theta  = bVect.heading();
      // precalculate trig values
      float sine = sin(theta);
      float cosine = cos(theta);

      /* bTemp will hold rotated ball positions. You 
       just need to worry about bTemp[1] position*/
      PVector[] bTemp = {
        new PVector(), new PVector()
        };

        /* this ball's position is relative to the other
         so you can use the vector between them (bVect) as the 
         reference point in the rotation expressions.
         bTemp[0].position.x and bTemp[0].position.y will initialize
         automatically to 0.0, which is what you want
         since b[1] will rotate around b[0] */
        bTemp[1].x  = cosine * bVect.x + sine * bVect.y;
      bTemp[1].y  = cosine * bVect.y - sine * bVect.x;

      // rotate Temporary velocities
      PVector[] vTemp = {
        new PVector(), new PVector()
        };

        vTemp[0].x  = cosine * velocity.x + sine * velocity.y;
      vTemp[0].y  = cosine * velocity.y - sine * velocity.x;
      vTemp[1].x  = cosine * other.velocity.x + sine * other.velocity.y;
      vTemp[1].y  = cosine * other.velocity.y - sine * other.velocity.x;

      /* Now that velocities are rotated, you can use 1D
       conservation of momentum equations to calculate 
       the final velocity along the x-axis. */
      PVector[] vFinal = {  
        new PVector(), new PVector()
        };

      // final rotated velocity for b[0]
      vFinal[0].x = ((m - other.m) * vTemp[0].x + 2 * other.m * vTemp[1].x) / (m + other.m);
      vFinal[0].y = vTemp[0].y;

      // final rotated velocity for b[0]
      vFinal[1].x = ((other.m - m) * vTemp[1].x + 2 * m * vTemp[0].x) / (m + other.m);
      vFinal[1].y = vTemp[1].y;

      // hack to avoid clumping
      bTemp[0].x += vFinal[0].x;
      bTemp[1].x += vFinal[1].x;

      /* Rotate ball positions and velocities back
       Reverse signs in trig expressions to rotate 
       in the opposite direction */
      // rotate balls
      PVector[] bFinal = { 
        new PVector(), new PVector()
        };

      bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y;
      bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x;
      bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y;
      bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x;

      // update balls to screen position
      other.position.x = position.x + bFinal[1].x;
      other.position.y = position.y + bFinal[1].y;

      position.add(bFinal[0]);

      // update velocities
      velocity.x = cosine * vFinal[0].x - sine * vFinal[0].y;
      velocity.y = cosine * vFinal[0].y + sine * vFinal[0].x;
      other.velocity.x = cosine * vFinal[1].x - sine * vFinal[1].y;
      other.velocity.y = cosine * vFinal[1].y + sine * vFinal[1].x;
    }
  }

相关问答

更多
  • 我会给你一个提示。 您在实现中将momentum乘以previous_weight ,这是同一步骤中网络的另一个参数。 这显然很快爆发了。 你应该做的是在前一个反向传播步骤中记住整个更新向量, l_rate * neuron['delta'] * inputs[j]并将其加起来。 它可能看起来像这样: velocity[j] = l_rate * neuron['delta'] * inputs[j] + momentum * velocity[j] neuron['weights'][j] += velo ...
  • 你可以只翻转一个标志,表示“一个球击中了一些砖并且应该改变它的方向”,然后在你的enterFrame处理程序中改变球的速度,而不是在removeBricks函数中改变球的速度: local ballHasCollided = false local function removeBricks(event) if event.other.isBrick == 1 then ballHasCollided = true end end local function updat ...
  • 首先,你的距离测试应该涉及两个对象,而不仅仅是一个(因为它,距离总是为零)注意使用position*[j] : distance = Math.sqrt(Math.pow(positionX[i] - positionX[j], 2) + Math.pow(positionY[i] - positionY[j], 2)); 你的反弹计算也是错误的。 对于最简单的反弹,只需撤消最新的速度调整,然后反转速度分量的符号(同样,您需要访问两个对象,而不仅仅是一个,注意radius[j] ): if (distan ...
  • 如果你忘记了这个公式,并且认为你能做某事的速度与你做这件事的时间成反比,那将更直观。 例如,如果你需要0.5小时吃比萨饼,你每小时吃2个比萨饼比萨饼,因为1 / 0.5 = 2。 在这种情况下,费率是每次交易的数量,时间是交易所需的时间。 根据问题,一个事务需要100次磁盘访问,每次磁盘访问需要20 ms。 因此,每笔交易总共需要2秒。 然后,该速率为每秒1/2 = 0.5次交易。 现在,更正式地说: 每秒的交易率R与以秒为单位的交易时间成反比TT。 R = 1/TT 在这种情况下,交易时间TT是: TT ...
  • 如果你想知道什么属性作为主要属性,我建议速度 。 如果选择动量,则RK4必须处理质量; 如果选择加速度,碰撞必须处理非常小的时间间隔和巨大的加速度; RK4已经具有速度,并且由于碰撞是必须处理特定物体及其质量的离散事件,因此将最终动量转换为最终速度非常容易。 If you're wondering what property to use as the primary one, I suggest velocity. If you choose momentum, then RK4 must deal wi ...
  • 设置v1nPost的行中是否有拼写错误? 看起来分母应该是this.mass + b.mass ,而不是this.mass * b.mass 。 另外,因为你正在计算this和b之间的碰撞,你是否正在检查以确保你没有在b和this之间做同样的碰撞,这样碰撞中的每个参与泡泡的delta增加了一倍? Is there a typo in the line that sets v1nPost? Looks like the denominator should be this.mass + b.mass, not ...
  • 你似乎把整个动量本身的个体动量的大小误认为是总和。 物质点的动量是矢量: p = m。 v 总动量是所有个体动量的矢量和: P =Σmi。 v i =mΣvi (在你的情况下,所有质量是相等的) 动能是标量: K =(1/2)m。 v 2 = p 2 /(2m) 并且总动能再次是所有单个动能的总和: K =Σ(1/2)m i 。 v i 2 =(m / 2)Σvi 2 在弹性碰撞中,K和P都是守恒的。 这在您的示例中也适用。 实际上,你的球的初始速度是(2,0)和(0,0),因此: P before = m ...
  • Processing附带的一个例子就是这样:使用动量守恒方程来计算多个球的速度。 这个例子在这里可用,或者您可以通过转到File - > Examples从Processing编辑器运行它。 该示例包含相当多的代码,但有趣的部分在这里: void checkCollision(Ball other) { // get distances between the balls components PVector bVect = PVector.sub(other.position, posi ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。