Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Thtread A creates a random number and add to the text area A, then notify the thread B. The thread B then add this number to the text area B and notify the thread A to create a new random number,… and continue like that.

Example:

Thread A Thread B
A create 51 B display 51
A create 43 B display 43
A create 29

My problem: My program ran successfully with numbers but I don't know how to display "A create + number" and "B display + number" like example.

What I have tried:

DefaultListModel ta,tb;
    Thread a, b; Random u;
    public JFrame1() {
        initComponents();
        ta = new DefaultListModel();
        text1.setModel(ta);
        tb = new DefaultListModel();
        text2.setModel(tb);
        u = new Random();
        initThreadA();
        initThreadB();
    }
    
void threadA_work() {
    String []ar = {"A create %d"};
       int x = u.nextInt(100);
       //ta.addElement("A create ");
       ta.addElement(x);
    }
    void threadB_work() {
        int size = ta.getSize();
        int x = (Integer) ta.elementAt(size - 1);
        //tb.addElement("B display ");
        tb.addElement(x);
    }

    void initThreadA() {
        a = new Thread() {
            public void run() {
                while(true) {
                    threadA_work();
                    try {
                        sleep(1000);
                    } catch(InterruptedException e) { } 
                    synchronized(b) {
                        b.notify();
                    }
                    try {
                        synchronized(this) {
                            wait();
                        } 
                    } catch(InterruptedException e) { }
                }    
            }  
        };  
    }

    void initThreadB() {
        b = new Thread() {
            public void run() {
                while(true) {
                    try {
                        synchronized(this) {
                            wait();
                        } 
                    } catch(InterruptedException e) { }
                    threadB_work();
                    try {
                        
                        sleep(1000);
                    } catch(InterruptedException e) {  }
                    synchronized(a) {
                        a.notify(); 
                    }
                }    
           }  
        };  
    }
    private void bt1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        a.start();
        b.start();  
    }   
Posted
Updated 21-Jan-19 5:48am

Java
String []ar = {"A create %d"};
   int x = u.nextInt(100);
   //ta.addElement("A create ");
   ta.addElement(x);

That does not make much sense. Why are you trying to create a string array with just a single line of text? And the "%d" item is usually used in a format string to be replaced with a numeric value. I suspect you actually want something like:
Java
String strText = String.format("A create %d", nextInt(100));
   ta.addElement(strText);
 
Share this answer
 
I had tried your solution's situation yet but Thread A ran but Thread B didn't work. Thread B can't get value from Thread A
 
Share this answer
 
Comments
Richard MacCutchan 21-Jan-19 12:19pm    
Please do not post comments or questions as Solutions.

I only gave you the code to correctly set the text string. What you are trying to do it thread B is to convert the string "A create N" to an integer. But it is text not a number so it will always fail. You need to change it so that it just contains numeric digits, without the preamble "A create ". I am not sure what you are actually trying to do with all this code but it seems far more complicated than it needs to be. Why are you using threads in the first place?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900