[英]run threads simultaneously
我有下一个代码:
public class App
{
public static void main( String[] args )
{
NewSmartThread n = new NewSmartThread("ST");
try{
n.join();
}catch(InterruptedException e){
System.out.println( "Умный поток был прерван" );
}
System.out.println( "Hello World!" );
}
}
class NewSimpleThread extends Thread {
public NewSimpleThread(String name){
super(name);
}
public void run(){
try {
for(int i =1; i<6; i++)
{
System.out.println("Thread " + this.getName() + " : " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Thread " + this.getName() + "был прерван.");
}
System.out.println("Thread " + this.getName() + " closed.");
}
}
class NewSmartThread extends Thread {
//final CountDownLatch start = new CountDownLatch(1);
//final CountDownLatch finish = new CountDownLatch(2);
public NewSmartThread(String name) {
super(name);
start();
}
public void run(){
//s1.split("(?<=\\G..)")
NewSimpleThread n1 = new NewSimpleThread("1");
NewSimpleThread n2 = new NewSimpleThread("2");
NewSimpleThread n3 = new NewSimpleThread("3");
n1.start();
n2.start();
n3.start();
try{
Thread.sleep(3000);
n1.join();
n2.join();
n3.join();
}catch(InterruptedException e){
System.out.println("Thread " + "broke.");
}
System.out.println("Smart thread " + this.getName() + " closed.");
}
}
我得到下一个结果:
> Thread 2 : 1
> Thread 3 : 1
> Thread 1 : 1
> Thread 3 : 2
> Thread 1 : 2
> Thread 2 : 2
> Thread 2 : 3
> Thread 1 : 3
> Thread 3 : 3
> Thread 2 : 4
> Thread 1 : 4
> Thread 3 : 4
> Thread 1 : 5
> Thread 2 : 5
> Thread 3 : 5
> Thread 1 closed.
> Thread 2 closed.
> Thread 3 closed.
> Smart thread ST closed.
> Hello World!
但是,如果我在下一个代码中使用块同步:
public class App
{
public static void main( String[] args )
{
NewSmartThread n = new NewSmartThread("ST");
try{
n.join();
}catch(InterruptedException e){
System.out.println( "Умный поток был прерван" );
}
System.out.println( "Hello World!" );
}
}
class NewSimpleThread extends Thread {
private static final Object monitor = new Object();
public NewSimpleThread(String name){
super(name);
}
public void run(){
synchronized (monitor) {
try {
monitor.wait();
for(int i =1; i<6; i++)
{
System.out.println("Thread " + this.getName() + " : " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread " + this.getName() + " closed.");
}
}
public static void doNotifyAll(){
synchronized (monitor) {
monitor.notifyAll();
}
}
}
class NewSmartThread extends Thread {
public NewSmartThread(String name) {
super(name);
start();
}
public void run(){
NewSimpleThread n1 = new NewSimpleThread("1");
NewSimpleThread n2 = new NewSimpleThread("2");
NewSimpleThread n3 = new NewSimpleThread("3");
n1.start();
n2.start();
n3.start();
try{
Thread.sleep(3000);
NewSimpleThread.doNotifyAll();
n1.join();
n2.join();
n3.join();
}catch(InterruptedException e){
System.out.println("Thread " + "broke.");
}
System.out.println("Smart thread " + this.getName() + " closed.");
}
}
并得到下一个结果:
Thread 3 : 1 Thread 3 : 2 Thread 3 : 3 Thread 3 : 4 Thread 3 : 5 Thread 3 closed. Thread 2 : 1 Thread 2 : 2 Thread 2 : 3 Thread 2 : 4 Thread 2 : 5 Thread 2 closed. Thread 1 : 1 Thread 1 : 2 Thread 1 : 3 Thread 1 : 4 Thread 1 : 5 Thread 1 closed. Smart thread ST closed. Hello World!
但是我需要像第一个示例中那样的结果。 并且不能使用CountDownLatch之类的某些功能-仅同步块(这是实验室实验,这是必需项)
有谁知道如何做到这一点?
如果我正确理解了问题,那么问题就出在第二个示例中,您如何使用同步块。
您已synchronized
了整个逻辑,包括同步块内的for
循环。 正确的方法是保持
synchronized(monitor){
monitor.wait(); //excluding exception handling
}
//for loop logic
因为您只希望线程等待主线程开始工作,并且一旦它们收到通知,您就不希望逻辑以synchronized
方式执行,因此释放监视器 。 因为您在for
循环时按住监视器,所以其他线程无法继续进行直到持有该监视器的线程完成。
感谢Narendra Pathai。
我的代码的结束变种如下:
public class App
{
public static void main( String[] args )
{
NewSmartThread n = new NewSmartThread("ST");
try{
n.join();
}catch(InterruptedException e){
System.out.println( "Умный поток был прерван" );
}
System.out.println( "Hello World!" );
}
}
class NewSimpleThread extends Thread {
private static final Object monitor = new Object();
public NewSimpleThread(String name){
super(name);
}
public void run(){
try {
Latch.awaitZero();
for(int i =1; i<6; i++)
{
System.out.println("Thread " + this.getName() + " : " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread " + this.getName() + " closed.");
}
public static void doNotifyAll(){
synchronized (monitor) {
monitor.notifyAll();
}
}
}
class NewSmartThread extends Thread {
public NewSmartThread(String name) {
super(name);
start();
}
public void run(){
NewSimpleThread n1 = new NewSimpleThread("1");
NewSimpleThread n2 = new NewSimpleThread("2");
NewSimpleThread n3 = new NewSimpleThread("3");
n1.start();
n2.start();
n3.start();
try{
Thread.sleep(3000);
Latch.doNotifyAll();
n1.join();
n2.join();
n3.join();
}catch(InterruptedException e){
System.out.println("Thread " + "broke.");
}
System.out.println("Smart thread " + this.getName() + " closed.");
}
}
class Latch {
private static final Object synchObj = new Object();
public static void awaitZero() throws InterruptedException {
synchronized (synchObj) {
synchObj.wait();
}
}
public static void doNotifyAll() {
synchronized (synchObj) {
synchObj.notifyAll();
}
}
}
我添加了课程:
class Latch {
private static final Object synchObj = new Object();
public static void awaitZero() throws InterruptedException {
synchronized (synchObj) {
synchObj.wait();
}
}
public static void doNotifyAll() {
synchronized (synchObj) {
synchObj.notifyAll();
}
}
}
代替
class NewSimpleThread extends Thread {
private static final Object monitor = new Object();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.