[英]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.