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 9115257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:23:16+00:00 2026-06-17T04:23:16+00:00

If you look at the code I have two ArrayLists that are called, pressed

  • 0

If you look at the code I have two ArrayLists that are called, pressed and released that are global. What I have to do is update those arrays with the keys that are to be pressed, then pass these updated arrays to my Thread, or update the thread.. this is the part where I’m a little lost on what I have to do.

The currently example(untested if runs), is a basic example of what I had in my actual program. When I ran it it would press the buttons once, then it would throw and error, I can’t remember the error as I can’t test it right now, but it had to do with the way I was using the thread.

QUESTION
How do I pass the arrays to my Thread, once the thread has been started.

Code Example:

import oscP5.OscEventListener;
import oscP5.OscMessage;
import oscP5.OscP5;
import oscP5.OscStatus;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Main implements OscEventListener {

protected BlockingQueue<Integer> _KeyQue = new ArrayBlockingQueue<>(1024);

Producer producer = new Producer(this._KeyQue);
Consumer consumer = new Consumer(this._KeyQue);
ThreadTest threadTest = new ThreadTest(this._KeyQue);

Thread prod;
Thread con;
Thread threadT;

OscP5 osc = new OscP5(this, 22556);

public static void main(String[] argv) {
    Main main = new Main();
    main.setup();
}

public void setup() {
    prod = new Thread(producer);
    con = new Thread(consumer);
    threadT = new Thread(threadTest);

    prod.start();
    con.start();
    threadT.start();

}

@Override
public void oscEvent(OscMessage theMessage) {
    float val = Float.parseFloat(theMessage.arguments()[0].toString());

    if (val == 1.0) {
        producer.addKey(KeyEvent.VK_W);
        producer.addKey(KeyEvent.VK_S);
    } else {
        consumer.removeKey(KeyEvent.VK_S);
    }
   threadTest.run();
}

@Override
public void oscStatus(OscStatus theStatus) {}

public class Producer implements Runnable {
    protected BlockingQueue<Integer> _KeyQue = null;

    public void addKey(int key) {
        try {
            this._KeyQue.put(key);
            System.out.println("Key " + key +" added to queue");
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }

    public Producer(BlockingQueue<Integer> _KeyQue) {
        this._KeyQue = _KeyQue;
    }

    public void run() {

    }
}

public class Consumer implements Runnable {
    protected BlockingQueue<Integer> _KeyQue = null;

    public void removeKey(int key) {
        try {
            this._KeyQue.remove(key);
            System.out.println("key " + key + " removed from queue");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public Consumer(BlockingQueue<Integer> _KeyQue) {
        this._KeyQue = _KeyQue;
    }

    public void run() {

    }
}

public class ThreadTest implements Runnable {

    protected BlockingQueue<Integer> _KeyQue = null;

    public ThreadTest(BlockingQueue<Integer> _KeyQue) {
        this._KeyQue = _KeyQue;
    }

    public void run() {
        try {
            Robot robot = new Robot();

            while(!this._KeyQue.isEmpty()) {
                for (Integer x : this._KeyQue) {
                    System.out.println("Keys in que: " + x);
                    Thread.sleep(500);
                }
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
}

Edit:
Ok, so I’ve taken a look at threads and BlockingQueue’s, but the thing I still can’t figure out is how to continue to run the ThreadTest’s run() method without locking the program. In this example it doesn’t run at all. When I directly call threadTest.run() it locks the program within it and doesn’t allow for adding or removing elements.

So what I need to do is be able to run a thread in the background that is constantly running, looping through *_KeysQueue()* and, in this example, printout the number associated with the keys. This should all happen while allowing me to add and remove keys.

  • 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-17T04:23:17+00:00Added an answer on June 17, 2026 at 4:23 am

    You can have a BlockingQueue and a method to add elements that would be called from every other thread. You can have a static method addKey that could be accessed from every other thread and that would look for adding the new key to the BlockingQueue.

    You can use there the producer-consumer pattern and you can see the book Java Concurrency In Practice or the link that led me to the book, in the blog The Java Specialists. The book has examples of all queues, concurrent or synchronized lists, ways to implement code to do several things, and all without having to stop to read 50 pages about something. An example and a few paragraphs of every issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this column called code that look like : 1-100-ETC-60 , 1-100-ETC-20 ,
I have code that I want to look like this: List<Type> Os; ... foreach
I often see a pattern used in circumstances where we have look-up code that
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have some parts in my code that look like this: A, B and
Basic question here - I have many lines of code that look something like:
I have two tables that look like this: node: NID Name 1 Chicago 2
I have two arrays, which can look like this: X = np.array([ 157, 262,
I have two integer arrays which contain numeric values. I want to look through
In my controller I have two actions called Friends. The one that executes depends

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.