在我的程序中,我有一个n项目列表.
我将迭代列表并启动这样的过程:
Runtime.getRuntime.exec("cmd /C start abc.bat"+listitem() )
Run Code Online (Sandbox Code Playgroud)
我需要保持4个进程的计数.完成任何一个过程后,我需要启动下一个过程,因此过程计数应为4.
我能够同时启动4个进程,但不确定如何保持4的计数.基本上我需要一个通知,一旦进程终止,所以我可以开始下一个,任何线程都是可能的.
有关如何实现这一点的任何帮助,有人可以分享上述要求的片段吗?
我最近开始深入研究Java中的多线程.在探索事物的过程中,我发现在Java中有两种快速而肮脏的方法可以"随时随地"创建Threads.这是一个例子:
public static void main(String[] args) {
System.out.println("Thread: " + Thread.currentThread().getName());
new Thread() {
public void run() {
System.out.println("Thread: "
+ Thread.currentThread().getName());
System.out.println("hello");
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread: "
+ Thread.currentThread().getName());
System.out.println("hello");
}
}).start();
}
Run Code Online (Sandbox Code Playgroud)
new Thread() {new Thread(new Runnable() {我只想问两种方法是否正确?除了实现Runnable Interface v/s扩展Thread类的差异之外还有什么区别?
我是多线程的新手.我正在尝试编写一个程序,我有两个线程.一个线程打印奇数,然后使用wait()放弃监视器锁定,同样其他线程打印偶数并在打印数字后放弃锁定
我有4个类
问题 - 我的代码大部分时间都按预期工作,即按顺序打印1到100.两个线程轮流.但是我注意到有一个bug.有时甚至线程会先安排好,然后输出低于输出
2 **********
1 ###############################
Run Code Online (Sandbox Code Playgroud)
之后没有任何内容被打印出来,看起来有一个死锁的情况.我无法弄清楚原因.请帮我理解这个
public class SomeMaths {
public synchronized void printOdd(){
for( int i=1;i<=100;i++){
if(i%2 !=0) {
System.out.println(i + " ###############################");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notify();
}
}
public synchronized void printEven(){
for(int i=1;i<=100;i++){
if(i%2 ==0){
System.out.println(i +" **********");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
notify();
}
}
}
Run Code Online (Sandbox Code Playgroud)
public class Odd implements Runnable {
SomeMaths sm; …Run Code Online (Sandbox Code Playgroud) 1)为什么Java语言同时提供Thread和Runnable?2)线程超过runnable有什么好处(为什么Java只能提供runnable)3)我们可以进行可运行的睡眠,给它一个id等吗?
这可能是一个基本问题,我对此感到困惑,
在一个文件中,我喜欢这样:
public class MyThread extends Thread {
@Override
public void run() {
//stuffs
}
}
Run Code Online (Sandbox Code Playgroud)
现在在另一个文件中我有这个:
public class Test {
public static void main(String[] args) {
Thread obj = new MyThread();
//now cases where i got confused
//case 1
obj.start(); //it makes the run() method run
//case 2
obj.run(); //it is also making run() method run
}
}
Run Code Online (Sandbox Code Playgroud)
所以在上面两种情况之间有什么区别,是情况1是创建一个新线程而情况2是不是创建一个线程?这是我的猜测...希望能更好地回答那些伙计们.谢谢
我正在研究TCP套接字.我从Thread派生我的服务器类.
public class TCPServer extends Thread {
public static int SERVERPORT = 54321;
....
<code>
}
Run Code Online (Sandbox Code Playgroud)
当我使用这个类时,它会打开几个线程.我的问题是,每个线程都有自己的静态变量SERVERPORT吗?因为看起来我编辑这个变量,它在其他变量中没有效果.
我对这个问题的解决方案是创建另一个类,比如说"GlobalVariables"并给它这个类访问它.
我对wait()方法的特定用例感到困惑.
根据javadoc,当出现下列情况之一时,wait应该结束:
在我正在等待的对象本身就是一个Thread的情况下,即使没有调用notify(),并且没有上述条件之一成立,也会发生wait()退出.但是当Thread.run()方法结束时会发生这种情况.虽然这种行为可能有意义,但不应该在Thread javadoc中记录吗?我发现它也很混乱,因为它与join()行为重叠.
这是我的测试代码:
public static class WorkerThread extends Thread {
@Override public void run() {
try{
System.out.println("WT: waiting 4 seconds");
Thread.sleep(4000);
synchronized (this) {
notify();
}
System.out.println("WT: waiting for 4 seconds again");
Thread.sleep(4000);
System.out.println("WT: exiting");
} catch (InterruptedException ignore) {
ignore.printStackTrace();
}
}
}
public static void main (String [] args) throws InterruptedException {
WorkerThread w = new WorkerThread();
w.start();
synchronized(w) {
w.wait();
System.out.println("MT: The object has been notified by the thread!");
}
synchronized(w) { …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,编译器建议我使用Thread.sleep(静态引用)而不是this.sleep,为什么会这样?
public class CThreadUsingThread extends Thread{
public void run(){
for (int i = 0; i < 10; i++) {
System.out.println("Thread running:"+i);
try {
// Why Thread.sleep(Math.round(Math.random()*1000)); is preferred?
this.sleep(Math.round(Math.random()*1000));
} catch (InterruptedException e) {
System.err.println("Thread interrupted!");
}
}
}
public static void main(String [] orgs){
CThreadUsingThread thread = new CThreadUsingThread();
thread.start();
}
}
Run Code Online (Sandbox Code Playgroud)

我有两个关于多线程编程的问题.我在网上看了几个答案,但我仍然找不到令人满意的答案.
实现Runnable比扩展线程类更受欢迎.为什么?
我们如何能够通过覆盖run()方法来逃脱?
根据Herbert Schildt的"The Complete Reference to Java",如果我们没有覆盖除run()之外的Thread类的任何方法,那么我们最好实现Runnable.
我的第二个问题可能听起来有点傻,但我似乎错过了一些东西,我不确定整个事情是如何运作的.
如果线程构造函数有参数,并且参数是一个函数入口,那么就相当于创建了一个新的线程来执行这个函数。不过,Thread也有无参构造函数,看来Thread类的成员函数都不能再绑定执行函数了。
问题:默认构造的线程对象有什么用?子线程如何执行?