Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8832913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:39:01+00:00 2026-06-14T08:39:01+00:00

I’m currently building a Java application using two threads : The first thread is

  • 0

I’m currently building a Java application using two threads :

The first thread is about the UI of the application, and also the processing of command received via the bluetooth thread.

The bluetooth thread is bluetooth server waiting for a robot to connect and handling communication.

As of now, the UI thread is in wait() state until the bluetooth thread gets a new message to process.

The problem is, I can trace the notify/notifyAll call from the bluetooth thread, but my UI is not resuming it’s activity.

I am now sure I misunderstood something about the proper way to manage synchronized threads, but I can’t figure out what’s wrong in my software.

Here is the code for the UI :

package mapper;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

public class MapperUI extends JFrame implements Runnable {

private ArrayList<String> messageArray;

public MapperUI(){
        super();        
        build();
        this.setVisible(true);
        new Thread(this).start();
}

private void build(){
    setTitle("SLAM Mapper");
    setSize(600,500);
    setLocationRelativeTo(null);
    setResizable(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setContentPane(buildContentPane());
}   
private JPanel buildContentPane(){
    JPanel main = new JPanel();
    main.setLayout(new BorderLayout());

    //TODO Implements auto-generated map after bluetooth communication
    MapPanel map = new MapPanel();
    main.add(map,BorderLayout.CENTER);

    //TODO This fields will be buildt with stored message
    JTable positions = new JTable(15,2);
    main.add(positions,BorderLayout.EAST);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());

    JButton bouton = new JButton("Start");
    buttonPanel.add(bouton);

    JButton bouton2 = new JButton("Send");
    buttonPanel.add(bouton2);

    main.add(buttonPanel,BorderLayout.SOUTH);

    return main;
}

public synchronized void run(){
    MapperCom bt = new MapperCom();
    while(true){
        try {
            System.out.println("Mapper is Waiting......");
            wait();
            String message = bt.getMessage();
            this.messageArray.add(message);
            bt.setNextCommand(processMessage(message));
            notifyAll();
            System.out.println("Mapper Notify");
            build();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }
}

public String processMessage(String message){
    String command = "";
    //TODO Build a response
    command = "fffff\n";
    return command;
}
}

Here is the bluetoothService :

package mapper;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class MapperCom extends Thread {
public final UUID uuid = new UUID(                              
        "27012f0c68af4fbf8dbe6bbaf7aa432a", false);
public final String name = "Server";                       
public final String url  =  "btspp://localhost:" + uuid         
                            + ";name=" + name 
                            + ";authenticate=false;encrypt=false;";
private LocalDevice local ;
private StreamConnectionNotifier server ;
private StreamConnection conn ;
private DataInputStream din ;
private DataOutputStream dout ;

private String command;
private String message;

public MapperCom(){
     try {
        this.command = "";
        this.message = "";

        System.out.println("Setting device to be discoverable...");
        local = LocalDevice.getLocalDevice();
        local.setDiscoverable(DiscoveryAgent.GIAC);
        System.out.println("Start advertising service...");

        server = (StreamConnectionNotifier)Connector.open(url);
        System.out.println("Waiting for incoming connection...\n");

        conn = server.acceptAndOpen();
        System.out.println("Client Connected...");
        din = new DataInputStream(conn.openInputStream());
        dout = new DataOutputStream(conn.openOutputStream());

        new Thread(this).start();

        } catch (Exception  e) {
            System.out.println("Exception Occured: " + e.toString());
        }
}

@Override
public synchronized void run(){
    System.out.println("Bluetooth Thread Started");
    while(true){
        try {
            String cmd = "";
            char c;
            System.out.println("Waiting for message");
            while (((c = din.readChar()) > 0) && (c!='\n') ){
                System.out.println("Char received :"+c);
                cmd = cmd + c;
            }

            storeMessage(cmd);

            System.out.println("Bt Notify......");
            notifyAll();
            System.out.println("Bt is Waiting for a command from mapper......");
            wait();
            sendResponse();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void sendResponse(){
    try {
        dout.writeChars(command);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public synchronized String getMessage(){
    return this.message;
}

public synchronized void storeMessage (String data){
    this.message = data;
    System.out.println("Received " + data);
}
public synchronized int setNextCommand (String data){
    int result = -1;
    //TODO Implement proper protocol
    this.command = data;
    System.out.println("Sending " + data);

    return result;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:39:03+00:00Added an answer on June 14, 2026 at 8:39 am

    Below is one sample program. Basically there needs some common lock between 2 threads if you want communication using wait & notify.

    package ravi.tutorial.java.threads;
    
    public class TestThreads {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            CommonLock commonLock = new CommonLock();
            Thread1 thread1 = new Thread1(commonLock);
            Thread2 thread2 = new Thread2(commonLock);
            thread1.start();
            thread2.start();
        }
    }
    
    /*
     * Common monitor lock between both threads, used for communication using wait
     * notify.
     */
    class CommonLock {
    
    }
    
    // Extending Thread instead of Runnable as its just a test
    class Thread1 extends Thread {
    
        private CommonLock commonLock;
    
        public Thread1(CommonLock commonLock) {
            this.commonLock = commonLock;
        }
    
        public void run() {
            System.out.println("Started thread 1");
            System.out.println("waiting thread 1");
            try {
                // TO wait on commonLock, first need to get lock on commonLock. SO
                // put synchronized block of commonLock.
                synchronized (commonLock) {
                    commonLock.wait();
                }
    
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("DONE waiting thread 1 as I got notification from THread 2");
        }
    
    }
    
    class Thread2 extends Thread {
    
        private CommonLock commonLock;
    
        public Thread2(CommonLock commonLock) {
            this.commonLock = commonLock;
        }
    
        public void run() {
            System.out.println("Running thread 2");
    
            try {
                System.out.println("Sleeping thread 2");
                // Just take gap of 2 sec before notifying.
                Thread.sleep(2000);
    
                // TO notify on commonLock, first need to get lock on commonLock. SO
                // put synchronized block of commonLock.
                synchronized (commonLock) {
                    System.out.println("Notifying thread 2");
                    commonLock.notifyAll();
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.