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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:25:35+00:00 2026-06-06T14:25:35+00:00

I have a java threading code. I have 3 threads, I have given names

  • 0

I have a java threading code. I have 3 threads, I have given names to all of them. 2 consumer threads name Consumer1 and Consumer2. A producer thread named Producer. I have a synchronized method where I try to compare the names of thread. I want that if thread name is consumer2, I want it to sleep for some time. Like so:

public synchronized String getReferer()
    {
        try{
            String threadName = Thread.currentThread().getName();
            System.out.println("Thread name: "+threadName);
            System.out.println(" compatre"+threadName=="Consumer2");
            if(threadName == "Consumer2"){
                System.out.println("It is consumer2. ...wait, you #$%^!!!");
                //this.wait();
            }
        }catch(Exception e){
            this.displayAndExit(e);
        }
        String line = "";
        int i = 0;
        try{
            finished = 0;
            if(this.hostnames2.isEmpty()){
                line = null;
            }else{
                line = this.hostnames2.take();
            }
        }catch(Exception e){
            this.displayAndExit(e);
        }
        return line;
    }
}

I start all thread one after another, first producer and the consumers. What happens is if I s.o.p threadName I can see Consumer1 and Consumer2 all over the place. But in second s.o.p I see something like this !!!

Thread name: Consumer2
false

Now since method is synchronized, a thread will hold lock onto it when it enters. So if it is Consumer2 then how come it’s comparison with string “Consumer2” fail when both statements are just back to back??

This is the main method and invocation of threads.

public class OffensiveFilterApplicator 
{
    public static void main(String [] args)
    {
        String pathToCSV = "src/unique_referer_dump3.txt";
        CSVResourceHandler csvResHandler = new CSVResourceHandler(pathToCSV);
        HostnameFileData hfd = new HostnameFileData();
        HostnameReader hr = new HostnameReader(hfd, csvResHandler.getCSVFileHandler());
        VexigoCallOps vco = new VexigoCallOps(hfd);
        Thread producer = new Thread(hr);
        Thread consumer1 = new Thread(vco);
        Thread consumer2 = new Thread(vco);
        producer.setName("Producer");
        producer.start();
        consumer1.setName("Consumer1");
        consumer2.setName("Consumer2");
        consumer1.start();
        consumer2.start();
    }
}
  • 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-06T14:25:37+00:00Added an answer on June 6, 2026 at 2:25 pm

    You can’t compare strings using ==. You have to use equals.

    Read more at
    What's the difference between ".equals" and "=="?

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

Sidebar

Related Questions

I have a question regarding threading in Java. In my code, ... client.doSth(); //
I have java script code to set some of the properties of ajax controls.
I have java source code in a text file. There has to be entered
I already have Java code to display and process data from a database. I
In my source tree have Java code which is automatically generated from XSD files.
I am using the ThreadPoolExecutor to implement threading in my Java Application. I have
programmer mates. I was testing java threading capabilities with a very simple code (or
Looks like I have messed up with Java Threads/OS Threads and Interpreted language. Before
I have a multi-threaded Java program with a bunch of rules around threading: For
I'm about to learn the java threading facility. I have 2 classes: public class

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.