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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:20:48+00:00 2026-06-03T14:20:48+00:00

I have a problem with Java. I would like to write a program where

  • 0

I have a problem with 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 i get the error of Illegal state in class Task:

while(suspended){
wait();
    System.out.println(character);
        }

The whole Code

import java.util.ArrayList;


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

public void run(){
    for(int i = 0; i < 1; i++){
        threads.add(new Thread(new Task(i+65)));
    }
    long cT = System.currentTimeMillis();
    for(int i = 0; i < threads.size(); i++){
        threads.get(i).start();
    }
    while(System.currentTimeMillis() - cT < 10000){
        for(int i = 0; i < threads.size(); i++){
            threads.get(i).start();
            try {
                Thread.sleep(1000);
            } catch (Exception e) {e.printStackTrace();
            }
            threads.get(i).stop();;
        }
    } 


}




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

}

}


public class Task implements Runnable {
int nr;
char character;
boolean suspended, resumed, stopped;
public Task(int literaASCII) {
    this.nr = 0;
    character = (char) (literaASCII);
    suspended = true;
    resumed = true;
    stopped = false;
}

@Override
public void run() {
    while(true){
        try{
        while(suspended){
            wait();
            System.out.println(character);
        }
        if(resumed){
            System.out.println("(Wznawiam watek litera: "+character+")");
            resumed = false;
        } 
        System.out.print(nr+""+character+", ");
        nr++;
        int r =  (int)((Math.random()*500) + 500);
        Thread.sleep(r);
        }catch(Exception e){e.printStackTrace();} 
    }
}

synchronized    public void suspend(){
    suspended = true;
    resumed = false; //chyba zbedne
}

synchronized    public void resume(){
    suspended  = false;
    resumed = true;
}


public static void main(String[] args) {
    // TODO Auto-generated method stub

}


}
  • 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-03T14:20:50+00:00Added an answer on June 3, 2026 at 2:20 pm

    someObject.wait() may only be invoked by a thread that is synchronized on someObject. The JavaDoc for wait mentions this:

    The current thread must own this object’s monitor.
    (source)

    In other words, this is broken:

    someObject.wait();
    wait();
    

    while this is valid:

    synchronized(someObject) {
        someObject.wait();
    }
    synchronized(this) {
        wait();
    }
    

    But the fact that you never call notify or notifyAll is suspicious.

    It sounds like what you’re trying to implement is a blocking queue: one thread puts items onto that thread, and the other thread takes them off it and processes them. If that’s the case, you should look into BlockingQueue.

    • 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 Threads in Java. I would like to write a
Hello I am a Java web app developer I have a problem, I would
I have a Java program that loads thirdparty class files (classes I did not
So I have a problem where Java is telling me that the following line
Good evening ladies and gentlemen, I have a problem with Java Swing that I
I have a problem with my Java progam suddenly exiting, without any exception thrown
I recently have a problem that my java code works perfectly ok on my
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
I have a problem with loading image with java 2ME. I have a image

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.