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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:55:19+00:00 2026-05-13T16:55:19+00:00

I understand threads in theory, but I have no idea how to implement them

  • 0

I understand threads in theory, but I have no idea how to implement them in Java.

setup

The circles are supposed to be threads and the rectangles are supposed to be buffers.

I have this all coded but it doesn’t work, so I am starting new. My source of confusion comes from the fact that I need this cycle to repeat many times and in this order, but I can’t predict what thread will run first. If thread B that relies in data from A runs first, what happens?

Also how can I keep the threads running indefinitely?

  • 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-13T16:55:19+00:00Added an answer on May 13, 2026 at 4:55 pm

    You can use Blocking Queues as buffers. They handle everything as far as getting threads to wait for other threads when the queues are empty.

    Basically you’ll have two classes, one for each thread. So, you’ll have something like this.

    class PageToRetriveQueue implements Runnable{
       PageBuffer partner;
       BlockingQeueue queue = new LinkedBlockingQueue<Page>();
    
       public void run(){
         while(true){
           Page p = partner.queue.take();
           for(Link l : p){
             queue.offer(l);
           }
         }
       }
    }
    
    class PageBuffer implements Runnable{
       PageToRetriveQueue partner;
       BlockingQeueue queue = new LinkedBlockingQueue<Link>();
    
       public void run(){
         while(true){
            Link l = partner.queue.take();
            Page p = downloadPage(l);
            queue.offer(p);
         }
       }
    }
    

    You’ll have to implement the Page, Link, and downloadPage functions. When you start, you’ll have to seed one of the queues in order to get started, probably the link queue. It’s stylistically bad form to call partner.queue.take() directly, rather you’d have a function that would abstract that. I’m trying to make the code concise and easy to understand here.

    Hope that helps!

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

Sidebar

Related Questions

I have read some threads about this already but I don't understand the code,
I understand that requests are served by different threads, but do they all come
I understand that Javascript doesn't have multiple threads, but I'd like to know if
I understand there are many threads like mine but I have been trying to
I am struggling to understand on java threads work so excuse this rather simple
I fail to understand why this code won't compile ExecutorService executor = new ScheduledThreadPoolExecutor(threads);
I tried to write a program that uses threads, but couldn't understand the o/p.
I'm trying to understand how threads works. I have some examples from the school.
Maybe the question sounds silly, but I don't understand 'something about threads and locking
I didn't fully understand the concept of threads I have some questions. Assume we

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.