简体   繁体   中英

so i am trying to print odd and even number using 2 threads but i am not getting the output i want, i want it to print in 1 to 10 in order

I am trying to learn Multi-threading and I am trying to print odd & even number using two thread but i am not sure how to synchronized the for loop and make it print from 1 to 10 in order.

public class Counter implements Runnable {

public static void main(String[] args) {
    Thread t1 = new Thread(new Counter(1, " ODD")); // Thread 1 runs the Odd number
    Thread t2 = new Thread(new Counter(0, " EVEN")); // Thread 2 runs the Even number

    t1.start();
    t2.start();

}

constructor:

int num; // gets the number 
String name; // gets the name

public Counter(int i, String name) {
    this.num = i;
    this.name = name;
}

This is the Loop im using to create Odd and Even number and i dont know how to synchronized this loop.

public void printNum() {
    synchronized (this) {
        for (int j = this.num; j <= 10; j += 2) {
            
            System.out.println(name + "-->" + j);
        }
        
    }
    
}

@Override
public void run() {
    //this will run the printNum to the Threads
    printNum();
}

Mb something like this

public class Counter implements Runnable{
@Override
public void run() {
    try {
        printNum();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
public static void main(String[] args){
    Thread t1 = new Thread(new Counter(1, " ODD")); // Thread 1 runs the Odd number
    Thread t2 = new Thread(new Counter(0, " EVEN")); // Thread 2 runs the Even number

    t2.start();
    t1.start();

}

int num; // gets the number
String name; // gets the name

public Counter(int i, String name) {
    this.num = i;
    this.name = name;
}

public void printNum() throws InterruptedException {
    synchronized (this) {
        for (int j = this.num; j <= 10; j += 2) {
            System.out.println(name + "-->" + j);
            Thread.sleep(100);
        }
    }
}

}

Result:

在此处输入图像描述

public class HelloWorld {
    public static void main(String[] args) {
        counter e = new counter();
        counter o = new counter();
        e.neighbor = o; 
        o.neighbor = e; 
        e.wait = false; 
        o.wait = true; 
        e.count = 0;
        o.count = 1;
        
        Thread te = new Thread(e);
        Thread to = new Thread(o);
        te.start();
        to.start();
    }
    
    static class counter implements Runnable{
        public counter neighbor = null; 
        public boolean wait = false; 
        public int count = -1;
        
        @Override
        public void run(){
            while (count <= 10){
                if (!wait){
                    System.out.print("count = " + count + "\n");
                    count += 2;
                    wait = true;
                    neighbor.wait = false;
                }
            }
            wait = true;
            neighbor.wait = false;
        }
    }
}

Often when you have two threads interdependent on each other, like in these case where odd needs to wait until even has finished and vice versa, we need to establish some kind of relation in order for them to communicate with each other, the reason why your code wasn't working was because synchronize makes the thread wait until the other one has finished, in the loop however, the entire loop is considered one task and one thread will wait for the other to finish their loop first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM