I have made a programe which print 5 number using 2 threads.
我做了一个程序,使用2个线程打印5个数字。
public class Class1 {
static int value = 6;
public void show() {
for (int i = 0; i < value; value--) {
System.out.println(Thread.currentThread().getName() + " - Number: " + value);
}
}
public static void main(String[] args) {
final Class1 main = new Class1();
Runnable runner = new Runnable() {
@Override
public void run() {
main.show();
}
};
new Thread(runner, "Thread 1").start();
new Thread(runner, "Thread 2").start();
} }
It gave me output like this:
它给了我这样的输出:
Thread 1 - Number: 6
Thread 2 - Number: 6
Thread 1 - Number: 5
Thread 2 - Number: 4
Thread 1 - Number: 3
Thread 1 - Number: 1
Thread 2 - Number: 2
But i want that it gave me output like this
但我希望它给了我这样的输出
Thread 1 - Number: 6
Thread 1 - Number: 5
Thread 1 - Number: 4
Thread 2 - Number: 3
Thread 2 - Number: 2
Thread 2 - Number: 1
I know it is easy we can simply use if else condition in the show() Method but i don't want to use if else in the show() method, i want to change some thing in the main() method which do the work done.
我知道很容易我们可以简单地在show()方法中使用if else条件,但是我不想在show()方法中使用if else,我想在main()方法中更改一些做的事情完成工作。
3 个解决方案
#1
1
You can wait to start the second thread after first thread has finished, basically you need to synchronize thread execution.
你可以等到第一个线程完成后启动第二个线程,基本上你需要同步线程执行。
To do that you can:
要做到这一点,你可以:
- enter in a
synchronized
block holding the same key - using the
wait
notify
keywords on the same lock - using the java
Lock
class - using
join
on first thread to wait that he finishes before starting the second - use an
Observable
to coordinate start of threads
输入一个包含相同密钥的同步块
在同一个锁上使用wait notify关键字
使用java Lock类
在第一个线程上使用join来等待他在开始第二个之前完成
使用Observable来协调线程的开始
And many others.
还有很多其他人。
#2
1
Firt thing first. You are not using synchronization in your Threads and if you want to use join()
you need to use an if
in your run method and
第一件事。您没有在线程中使用同步,如果要使用join(),则需要在run方法中使用if
static int value = 6;
Is not thread safe! For better use an AtomicInteger and for control of theads check ExecutorService
不是线程安全的!为了更好地使用AtomicInteger和控制theads,请检查ExecutorService
#3
1
You can do it like below:
你可以这样做:
final int number = 6;
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i< number/2;i++){
System.out.println("Thread 1 Number: "+i);
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = number/2; i< number;i++){
System.out.println("Thread 2 Number: "+i);
}
}
});
t1.run();
t2.run();
#1
1
You can wait to start the second thread after first thread has finished, basically you need to synchronize thread execution.
你可以等到第一个线程完成后启动第二个线程,基本上你需要同步线程执行。
To do that you can:
要做到这一点,你可以:
- enter in a
synchronized
block holding the same key - using the
wait
notify
keywords on the same lock - using the java
Lock
class - using
join
on first thread to wait that he finishes before starting the second - use an
Observable
to coordinate start of threads
输入一个包含相同密钥的同步块
在同一个锁上使用wait notify关键字
使用java Lock类
在第一个线程上使用join来等待他在开始第二个之前完成
使用Observable来协调线程的开始
And many others.
还有很多其他人。
#2
1
Firt thing first. You are not using synchronization in your Threads and if you want to use join()
you need to use an if
in your run method and
第一件事。您没有在线程中使用同步,如果要使用join(),则需要在run方法中使用if
static int value = 6;
Is not thread safe! For better use an AtomicInteger and for control of theads check ExecutorService
不是线程安全的!为了更好地使用AtomicInteger和控制theads,请检查ExecutorService
#3
1
You can do it like below:
你可以这样做:
final int number = 6;
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = 0; i< number/2;i++){
System.out.println("Thread 1 Number: "+i);
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int i = number/2; i< number;i++){
System.out.println("Thread 2 Number: "+i);
}
}
});
t1.run();
t2.run();