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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:34:45+00:00 2026-05-26T20:34:45+00:00

Constructor and method not working as expected in Java program I have the following

  • 0

Constructor and method not working as expected in Java program

I have the following code:

package principal;
public class Principal {
public static void main(String[] args) {
    Thread filosofos[]=new Thread[5];
    for (int i=0;i<5;i++) {
        System.out.println("loop");
        filosofos[i]=new Thread();
        filosofos[i].start();
    }
    // TODO Auto-generated method stub
}
}

package principal;
public class Filosofo implements Runnable{
static final int tamanho=5;
static int talheres[]=new int[tamanho];
static Semaforo semaforo= new Semaforo(1);
static int quantidade=0;
int id;
public Filosofo(){
    System.out.println("Construtor iniciado.");
    for (int i=0;i<tamanho;i++) {
        talheres[i]=0;
    }
    quantidade++;
    id=quantidade;
}
public void run () {
    System.out.println("Filosofo "+id+" iniciado");
    try {
        // Filosofo pensando
        Thread.sleep(1000);
    } catch (Exception e) {

    }
    semaforo.down();
    System.out.println("Filosofo "+id+" comendo");
    semaforo.up();
}
}

The program should exhibit the string “Construtor iniciado.” and the other 2 strings of method run. However when I run the code nothing happens only output that I receive is

loop
loop
loop
loop
loop

why the string of the constructor is not showing up? Why the method run is not running as expected? It looks like the constructor and the method run are not running at all, and I don’t know what is going wrong.

  • 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-26T20:34:45+00:00Added an answer on May 26, 2026 at 8:34 pm

    You have declared class Filosofo but you never create a single instance of it.

    Perhaps you want to pass a new instance of Filosofo as thread constructor parameter for each thread?

    package principal;
    
    public class Principal
    {
        public static void main(String[] args)
        {
            Thread filosofos[]=new Thread[5];
    
            for (int i=0;i<5;i++) {
                filosofos[i]=new Thread(new Filosofo());
                filosofos[i].start();
            }
        }
    }
    

    Except this, instead of using a static field for counting Filosofo instances and assign them an id, why you don’t just pass the id in the constructor?

    Also the other fields, don’t need to be static, pass the shared fields, like semaforo, in the constructor and copy them in a class field.

    I don’t know the meaning of talheres field and I don’t understand why you reinitialize a static field in each instance constructor, maybe you can just initialize once in main and pass that field in the constructor of each Filosofo, as you know, arrays are not copied, only a reference to them is copied.

    Also instead of catch (Exception e) you should use catch (InterruptedException e).
    You should do something useful with the exception, like printing it.
    If you intend to ignore an exception at least you should add a very detailed comment on why you are doing that.

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

Sidebar

Related Questions

I have following code and it is working fine. public partial class MainWindow :
Working on my assignment for Java and I have created a class called Triangle.
The following code was generated automatically by Netbeans 6.8 Mac version public class fileBrowser
clone method vs copy constructor in java. which one is correct solution. where to
The following method is launched from the constructor of UserControl. A cross thread exception
I know that calling a virtual method from a base class constructor can be
I have a simple class Name: class Name { private: char nameStr[30]; public: Name(const
I am working on my first Java program as a piece of homework. The
I'm working a program where there's an Airline class. The Airline class contains a
I'm working on a resource management class and want to have the user provide

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.