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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:28:42+00:00 2026-06-03T23:28:42+00:00

I have a problem with Threads in Java. I would like to write a

  • 0

I have a problem with Threads in Java.
I would like to write a program where there is Class Main which has ArrayList of Threads of some class (Class Task) which just writes a letter and the number. Object Main just wakes one Thread from ArrayList and let it to do something while the same object(Main) sleeps another one.
But there is one problem even if I change the Main.ACTIVE to false it does not end all of the Threads some stay on, and it’s random, I just would like to make them end and write:

I am saying goodbay + character – sth like that

public class Main extends Thread {
    ArrayList<Thread> threads;
    static boolean ACTIVE = true;
    public Main() {
        super();
        threads = new ArrayList<Thread>();
    }

    public void run(){

        Object monitor = new Object();
        for (int i = 0; i <= 5; i++) {
            threads.add(new Thread(new Task(i + 65, monitor)));
        }

        long cT = System.currentTimeMillis();
        for (int i = 0; i < threads.size(); i++) {
            threads.get(i).start();
        }
        System.out.println("BEFORE synchronized(monitor)");
        synchronized(monitor){
            while (System.currentTimeMillis() - cT < 1000) {
                try{
                    monitor.notify();
                    Thread.sleep(50);
                    monitor.wait();
                } catch(Exception e){
                    e.printStackTrace();}
                }
                System.out.println("BEFORE ACTIVE= FALSE and after WHILE in Main");
                ACTIVE = false;
                for(int i  = 0; i < threads.size(); i++){
                    System.out.println(threads.get(i).getState());
                }
            }
            System.out.println("LAST COMMAND IN MAIN");
        }
    }

    public static void main(String[] args) {
        new Main().start();
        //new Thread(new Task(65)).start();
    }
}

And the Task Class

public class Task implements Runnable {
    int nr;
    char character;
    Object monitor;

    public Task(int literaASCII, Object monitor) {
        this.nr = 0;
        this.monitor = monitor;
        character = (char) (literaASCII);
    }

    @Override
    public void run() {
        synchronized (monitor) {
            while (Main.ACTIVE) {
                try {
                     System.out.println("ENTERING WHILE IN TASK");
                    monitor.wait();
                    System.out.print(nr + "" + character + ", ");
                    nr++;
                    int r = (int) ((Math.random() * 50) + 50); // <500ms,1000ms)
                    Thread.sleep(r);
                } catch (Exception e) {e.printStackTrace();}
                monitor.notify();
                 System.out.println("YYYYYYYYY");
            }
             System.out.println("AFTER WHILE IN Task");
        }
        System.out.println("I am saying goodbye " + character);
    }
}
  • 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-03T23:28:43+00:00Added an answer on June 3, 2026 at 11:28 pm

    Now naswer is

    0A, 0B, 0C, 0D, 0E, 0F, 1A, 1B, 1C, 1D, 1E, 1F, WAITING
    WAITING
    WAITING
    WAITING
    WAITING
    WAITING
    LAST COMMAND IN MAIN

    I added sleep after starting threads

    import java.util.ArrayList;
    
    public class Main extends Thread {
    ArrayList<Thread> threads;
    volatile static boolean ACTIVE = true;
    public Main() {
        super();
        threads = new ArrayList<Thread>();
    }
    
     public void run(){
    
    Object monitor = new Object();
    for (int i = 0; i <= 5; i++) {
      threads.add(new Thread(new Task(i + 65, monitor)));
    }
    
    long cT = System.currentTimeMillis();
    for (int i = 0; i < threads.size(); i++) {
      threads.get(i).start();
    }
    try{Thread.sleep(50);}catch(Exception e){e.printStackTrace();}
     //   System.out.println("BEFORE synchronized(monitor)");
    synchronized(monitor){
      while (System.currentTimeMillis() - cT < 1000) {
          try{
    
        monitor.notify();
        Thread.sleep(500);
    
        monitor.wait();}catch(Exception e){e.printStackTrace();}
      }
       //    System.out.println("BEFORE ACTIVE= FALSE and after WHILE in Main");
      ACTIVE = false;
      for(int i  = 0; i < threads.size(); i++){
          System.out.println(threads.get(i).getState());
      }
    
    
    }
    System.out.println("LAST COMMAND IN MAIN");
    
      }
    
    
      public static void main(String[] args) {
         new Main().start();
        //new Thread(new Task(65)).start();
    
    }
    
    }
    

    and the TASK

    public class Task implements Runnable {
    int nr;
    char character;
    Object monitor;
    
    public Task(int literaASCII, Object monitor) {
        this.nr = 0;
        this.monitor = monitor;
        character = (char) (literaASCII);
    }
    
    @Override
    public void run() {
        synchronized (monitor) {
            while (Main.ACTIVE) {
                try {
    //               System.out.println("ENTERING WHILE IN TASK");
                    monitor.wait();
                    System.out.print(nr + "" + character + ", ");
                    nr++;
                    int r = (int) ((Math.random() * 50) + 50); // <500ms,1000ms)
    
                    Thread.sleep(r);
                } catch (Exception e) {e.printStackTrace();}
                monitor.notify();
        //       System.out.println("YYYYYYYYY");
            }
             System.out.println("AFTER WHILE IN Task");
        }
        System.out.println("I am saying goodbye " + character);
    }
    

    }

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

Sidebar

Related Questions

I have a problem with Java. I would like to write a program where
I have a problem with Java. I would like to write a program where
I have a problem with Observer-Pattern and deadlock using threads. package observerDeadLock; import java.util.Observable;
I have a problem with winform application which uses threads to update the UI.
I would like to implement a thread pool in Java, which can dynamically resize
I have a piece of running java software that is jammed. I would like
I would like to have some advices because I've got conflict between clients sending
If I have a class that implements java.lang.Runnable , there is technically nothing stopping
My problem is this: I have two threads, my UI thread, and a worker
The problem is this: I have multiple competing threads (100+) that need to access

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.