我有一个简单的function1,它向google api发出http请求并返回$ result.然后我有另一个函数2,如果$ result isset,应该使用$ result进行一些计算,然后返回$ finalresult..
我的问题是,对google api的调用需要几秒钟,而当function1返回$ result时,function2已经返回$ finalresult而不考虑$ result.
我要做的是让function1完全运行并在function2开始之前返回$ result.
我正在寻找一种不仅仅使用"sleep()"的解决方案,因为这个函数不能保证实际返回$ result.(除非有某种方法可以循环睡眠(1)直到$ return isset,或类似的东西)
视觉女孩和男人的示例代码
function1_geocode($address); // this function makes a http request to google and returns $result
function2_proximitysearch(){
if (isset($result)){
//inevitably by the time the script gets there, $result hasn't been returned yet therefore none of the "some stuff" code is executed.
//some stuff
}
else {
//some other stuff
}
}
Run Code Online (Sandbox Code Playgroud) 在java中编写多线程Internet服务器时,主线程启动新线程以并行处理传入请求.
如果主线程不等待(带.join())它们会有什么问题吗?(显然荒谬的是创建一个新线程然后等待它).
我知道,在实际情况下,您应该(或"必须"?)实现一个线程池,以便在它们空闲时"重新使用"它们以获取新请求.
但是对于小型应用程序,我们应该使用一个线程池吗?
我有一个简短的问题,我希望有人可以帮助我.
请查看以下代码段:
public void mouseEntered(MouseEvent e){
//wait 2 seconds.
//if no other mouseEntered-event occurs, execute the following line
//otherwise restart, counting the 2 seconds.
foo();
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题吗?我想实现像ToolTip这样的行为:你用鼠标进入一个区域.如果您的鼠标停留在该位置,请执行某些操作.
我在主循环的代码中有这个(窗口中的2D游戏):
try{
synchronized(this){
wait(3);
}
}
catch(Exception ex) {
System.out.println(ex);
}
Run Code Online (Sandbox Code Playgroud)
这段代码导致FPS上限为64,我不知道为什么.我不使用任何其他同步块.有趣的是,当Web浏览器打开时,fps不再受限制.谁能告诉我如何摆脱64 fps的限制?我没有设法找到这个问题的任何其他主题.
编辑:
浏览器怎么能改变fps?
所以我的问题是我需要这些方法一个接一个地运行,但我无法弄清楚如何使方法在运行之前等待.任何帮助表示赞赏.谢谢.这是我的代码:
public void startMoving() throws InterruptedException
{
moveEnemy("right",3);
wait();
moveEnemy("down",3);
wait();
moveEnemy("right",2);
wait();
moveEnemy("up",1);
wait();
moveEnemy("right",2);
wait();
moveEnemy("up",2);
wait();
moveEnemy("right",2);
wait();
moveEnemy("down",4);
wait();
moveEnemy("left",1);
wait();
moveEnemy("down",2);
wait();
moveEnemy("right",3);
wait();
moveEnemy("up",2);
wait();
moveEnemy("right",1);
wait();
moveEnemy("up",1);
wait();
moveEnemy("right",3);
}
public void moveEnemy(final String direction, final int numMoves)
{
Thread moveThread = new Thread(new Runnable()
{
public void run()
{
isMoving = true;
int originalX = getX();
int originalY = getY();
for(int loop = 0; loop <= 98*numMoves; loop++)
{
try
{
Thread.sleep(5); …Run Code Online (Sandbox Code Playgroud) 我的进度条最初设置为INVISIBLE.单击按钮后,我希望条形显示5秒而不是消失.我正在使用Thread.sleep(5000)但没有任何反应
public void dec(View v) throws InterruptedException{
ProgressBar pbar = (ProgressBar) findViewById(R.id.bar);
pbar.setVisibility(View.VISIBLE);
Thread.sleep(5000);
pbar.setVisibility(View.INVISIBLE);}
Run Code Online (Sandbox Code Playgroud) 我希望知道在synchronized块中写入的代码但是在wait()语句实际以同步方式运行之后,因为在wait()期间放弃了锁定,一旦线程被通知它再次需要锁定.
public void process(){
synchronized(sharedObj){
try{
sharedObj.wait();
}catch(InterruptedException ex){
ex.printStackTrace();
}
// when wait is over do the line below runs in synchronized way. It is inside synchronized block but during call of wait(), lock was relinquished.
sharedObj.writeValue();
}
}
Run Code Online (Sandbox Code Playgroud)
请帮我澄清一下我的疑问.
此致,克里希纳
我是c#异步等待机制的新手.我一直在阅读一些关于异步的文章(http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html).我在下面有一个例子,如果第Initialize一种方法会导致死锁,为什么还能告诉我?先感谢您.
public class Test {
public async Task DoSomeWorkAsync() {
await DoSomeWork1Async();
}
// This method is mixed with Wait and async await, will this cause lead lock?
public void Initialize() {
Task.Run(() => DoSomeWorkAsync()).Wait();
}
// This method is following async all the way
public async Task InitializeAsync() {
await DoSomeWorkAsync();
}
}
// Update: Here is the context where two Initialize methods are called
public class TestForm : Form {
// Load Form UI …Run Code Online (Sandbox Code Playgroud) 我有一个for循环,我想一次并行处理4次。我从页面https://unix.stackexchange.com/questions/103920/parallelize-a-bash-for-loop尝试了以下代码:
task(){
sleep 0.5; echo "$1";
}
N=4
(
for thing in a b c d e f g; do
((i=i%N)); ((i++==0)) && wait
task "$thing" &
done
)
Run Code Online (Sandbox Code Playgroud)
我将上面的文件存储为test.sh,我得到的输出如下:
path$ ./test.sh
a
b
c
d
path$ e
f
g
Run Code Online (Sandbox Code Playgroud)
并且光标在'g'之后没有返回到我的终端,它会无限期地等待/睡眠。我希望光标回到我的终端,并且我也不明白为什么输出'e'前面有我的路径,不应将输出连续显示为从“ a”到“ g”并且代码应该停止吗?
更新后的版本
我试图找到一种更动态的方式来等待元素而不是使用静态等待函数 Task.Event(2000).Wait();
对此的解决方案似乎是这样的:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[2]/div/input")));
Run Code Online (Sandbox Code Playgroud)
但是当我使用这个函数时,"ExpectedConditions"总是亮起红色,表示它:"当前上下文中不存在".
我把这个函数放在我的一个测试用例中:
(我使用的是C#/ Visual Studios,项目类型:Classlibrary)
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary1
{
public class MyFirstTest
{
IWebDriver driver = new FirefoxDriver();
[Test]
public void WaitverifyTest()
{
driver.Navigate().GoToUrl("https://www.google.se/");
driver.Manage().Window.Maximize();
Task.Delay(4000).Wait();
driver.FindElement(By.XPath("//div[2]/div/input")).SendKeys("Selenium");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[2]/div/input")));
element.Click();
driver.FindElement(By.XPath("//center/input")).Click();
}
}
}
Run Code Online (Sandbox Code Playgroud)
(xpath是一个位置类型xpath,它是有效的,可以在Selenium IDE和Visual Studios中使用.)
任何人都知道我做错了什么?
我有两个怀疑: