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

  • SEARCH
  • Home
  • 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 6922831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:27:26+00:00 2026-05-27T10:27:26+00:00

Okay been working on this too long and can’t get it to work. I

  • 0

Okay been working on this too long and can’t get it to work. I have a list of Thread types that can be different classes i.e. WriteFileData(extends Thread). I want to for loop through that list and do a call to add to queue a byte array. I currently have this in a Broker class

// consumers is filled with different Thread types all having a queue
// variable of type LinkedBlockingQueue
ArrayList<Thread> consumers = new ArrayList<Thread>();

synchronized void insert(final byte[] send) throws InterruptedException {
    for (final Thread c : consumers) {
        if (c instanceof WriteFileData) {
            ((WriteFileData)c).queue.add(send);
        } 
        ...other class threads...
}

but what I want to do is something more along the lines of

synchronized void insert(final byte[] send) throws InterruptedException {
    for (final Thread c : consumers) {
       Class<?> cls = Class.forName(c.getClass().getName());
       Field field = cls.getDeclaredField("queue");
       Class<?> cf = Class.forName(field.getType().getName());
       Class[] params = new Class[]{Object.class};

       Method meth = cf.getMethod("offer", params);

       meth.invoke(cf, send);  // errors at this line....

EDIT: Fixed “method not found error” but now can’t seem to invoke method due to I’m sending it an Array and its method wants just an Object.

…. alas it errors out at meth.invoke. Not sure how to do this as this is many levels deep, I wanted to use the add method on queue but that is one more layer of class abstraction.

Here is what WriteFileData has…

public class WriteFileData extends Thread {
    LinkedBlockingQueue<byte[]> queue = new LinkedBlockingQueue<byte[]>();

...
}
  • 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-05-27T10:27:27+00:00Added an answer on May 27, 2026 at 10:27 am

    Here is what I did with some advice from @Erik.

    Here is WriteFileData with added method add(byte[] send) …

    public class WriteFileData extends Thread {
        private LinkedBlockingQueue<byte[]> queue = new LinkedBlockingQueue<byte[]>();
    
        public void add(byte[] send) {
            queue.add(send);
        }
    
    ...
    }
    

    And now my Broker class method looks like this:

    public synchronized void insert(final byte[] send) {
        for (final Thread c : consumers) {
        try {
            Class<?> cls = Class.forName(c.getClass().getName());
            Method meth = cls.getMethod("add", new Class[]{byte[].class});
            meth.invoke(c, send);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I been trying to work this out but unable too. I have a
Okay I have been working on this for about 2 hours and am sure
Okay so i have been working on this app for a couple months now
Okay this one has me stumped.. mainly because i have been working on this
Okay. Now I give up. I have been playing with this for hours. I
Okay I have been working on a website but I keep on having an
Okay, so I have been using a PDO wrapper for a project I'm working
Okay, I'm working on a problem that I've been holding off on for three
Okay so here's the lowdown... been working on this solution for the past week
I've been trying to get this complex MYSQL query to work exactly right over

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.