检查Internet连接的可用性+两个线程之间的通信(Check availability of Internet Connection + Communicate between 2 threads)
我有两个问题:
如何检查Internet连接是打开还是关闭? 我正在使用Html Unit,我正在使用Windows。
我想制作一个JLabel,说明我的JFrame中的互联网连接的可用性。 就像是:
while(true) { if(isOnline()) label.setText("online"); else label.setText("offline"); }
但我认为我需要2个备份线程,但是我怎样才能创建这2个线程并在它们之间进行通信并实现这一目标?
I have 2 questions:
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.
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
最满意答案
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; } }
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
用动量反向传播(Backpropagation with Momentum)[2023-08-28]
我会给你一个提示。 您在实现中将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 ...
-
关于Littles Law的问题(question about Littles Law)[2023-07-23]
如果你忘记了这个公式,并且认为你能做某事的速度与你做这件事的时间成反比,那将更直观。 例如,如果你需要0.5小时吃比萨饼,你每小时吃2个比萨饼比萨饼,因为1 / 0.5 = 2。 在这种情况下,费率是每次交易的数量,时间是交易所需的时间。 根据问题,一个事务需要100次磁盘访问,每次磁盘访问需要20 ms。 因此,每笔交易总共需要2秒。 然后,该速率为每秒1/2 = 0.5次交易。 现在,更正式地说: 每秒的交易率R与以秒为单位的交易时间成反比TT。 R = 1/TT 在这种情况下,交易时间TT是: TT ... -
加速或动力(Acceleration Or Momentum)[2023-06-26]
如果你想知道什么属性作为主要属性,我建议速度 。 如果选择动量,则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 ...