据我所知,sleep() 用于让线程 hibernate 指定时间。我做了两个示例 - 在示例 1 中,我得到的输出为 1,2, 3,4 因为我只创造了一个。在示例 2 中,我创建了线程的 2 个实例,并得到输出 1,1,2,2,3,3,4,4。
为什么第一个线程的输出不是 1、2、3、4,然后是 1,2,3,4 是第二个吗?
示例 1:
// Using sleep() method
public class Aaa extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{
Thread.sleep(500);
} catch(InterruptedException e){
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String[] args){
Aaa m1=new Aaa();
m1.start();
}
}
Output:
1
2
3
4
示例 2:
// Using sleep() method
public class Aaa extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{
Thread.sleep(500); // sleeps thread
} catch(InterruptedException e){
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String[] args){
Aaa m1=new Aaa(); // creating one object
Aaa m2=new Aaa(); // creating second object of a class
m1.start(); // calls run method
m2.start();
}
}
Output:
1
1
2
2
3
3
4
4
最佳答案
您已经创建了两个 Runnable 对象。如果你通过调用它们的 run 方法来运行它们,你会得到你想象的结果:
Aaa m1=new Aaa(); // creating one object
Aaa m2=new Aaa(); // creating second object of a class
System.out.println("Calling m1.run()");
m1.run(); // we call run method
System.out.println("Calling m2.run()");
m2.run();
输出
Calling m1.run()
1
2
3
4
Calling m2.run()
1
2
3
4
因为方法执行了两次,一个接一个。
请注意,调用Runnable/Thread 的run 方法不是运行线程的正常方法。您通常会使用 start 方法。
但是,如果您将每个都放在一个Thread 中并启动 它们:
Aaa m1=new Aaa(); // creating one object
Aaa m2=new Aaa(); // creating second object of a class
System.out.println("Calling m1.start()");
m1.start(); // thread calls run method
System.out.println("Calling m2.start()");
m2.start();
现在每个对象都在自己的线程中并行运行,因此输出是交错的:
Calling m1.start()
Calling m2.start()
1 < From thread 1
1 < From thread 2
2 ...
2
3
3
4
4
顺序显然会因线程交错方式而异。
可能会让您感到困惑的一件事是您选择了extend Thread。这是气馁。最好是 implement Runnable - 像这样:
public class Aaa implements Runnable {
public void run() {
for (int i = 1; i < 5; i++) {
try {
Thread.sleep(500); // sleeps thread
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println(i);
}
}
public static void main(String[] args) {
Aaa m1 = new Aaa(); // creating one object
Thread t1 = new Thread(m1); // Its thread
Aaa m2 = new Aaa(); // creating second object of a class
Thread t2 = new Thread(m2); // Its thread
t1.start(); // calls m's run method in a new thread.
t2.start();
}
}
现在更清楚您的对象在两个不同的线程中运行,因此并行运行。
关于java - sleep() 方法如何在给定线程和输出上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19092556/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/