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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:16:33+00:00 2026-05-14T20:16:33+00:00

public static synchronized void main(String[] args) throws InterruptedException { Thread t = new Thread();

  • 0
  public static synchronized void main(String[] args) throws InterruptedException {
    Thread t = new Thread();
    t.start();
    System.out.print("X");
    t.wait(10000);
    System.out.print("Y");
  }
  • What is the problem with this method?
  • How can I avoid such problems from now on?
  • 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-14T20:16:34+00:00Added an answer on May 14, 2026 at 8:16 pm

    There are a couple of problems with this code. I suspect you’re trying to write something like this:

    public static synchronized void main(String[] args) throws InterruptedException {
      System.out.print("X");
      Thread.sleep(10000);
      System.out.print("Y");
    }
    

    The Thread.sleep() method will suspend the current thread for the specified interval. Object.wait() is something else entirely and it’s unlikely that that’s what you want.

    You can see how I’ve eliminated the thread t. If you really want to create a separate thread and have the printouts produced on that thread then you need to give the thread something to do. The easiest way to do that is to override the thread’s run() method and have the thread code there:

    public static synchronized void main(String[] args) {
      Thread t = new Thread() {
        public void run() {
          System.out.print("X");
          try { Thread.sleep(10000); } catch (InterruptedException e) { }
          System.out.print("Y");
        }
      };
    
      t.start();
    }
    

    As written your original code was in fact creating a thread with no thread body, so when you call t.start() the empty thread would simply start up in the background and then immediately die.

    Note that I had to add a try/catch clause for InterruptedException now that the sleep call has migrated to inside the thread. run() isn’t allowed to throw exceptions so now, unfortunately, we have to catch and ignore the exception.

    Another way to write this would be to do some of the work in the thread t and the rest of the work in your main thread. Here’s an example of how you could split apart the work into two threads:

    public static synchronized void main(String[] args) throws InterruptedException {
      Thread t = new Thread() {
        public void run() {
          System.out.print("X");
          try { Thread.sleep(10000); } catch (InterruptedException e) { }
        }
      };
    
      t.start();
      t.join();
    
      System.out.print("Y");
    }
    

    When this calls t.join() it will wait for the thread to finish executing, which will take 10 seconds since it’s sleeping. Once the thread is finished then the join() method will return and allow the main thread to continue. The end result will appear the same to the user: the program will print X, pause for 10 seconds, and then print Y.

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

Sidebar

Related Questions

public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
class ZiggyTest{ public static void main(String[] args){ RunnableThread rt = new RunnableThread(); Thread t1
import java.io.*; public class ThreadSanbox { public static void main(String[] args) { Thread t1
public class Main { public static void main(String[] args){ XClass x = new XClass();
public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader br1 =
public static void main(String Data[]) { ConnectionPoolDataSource dps; try { cnt=new InitialContext(); cnt.rebind(java:comp/env/jdbc/pool/dragon, dps);
Given this code: public class Messager implements Runnable { public static void main(String[] args)
public static <AnyType> void mysteryPrint(BinaryNode<AnyType> t) { if( t! = null) { System.out.println(t.getElement() );
Class ThreadTest extends Thread { public synchronized void run() { } public static void
Demo code: import java.lang.ref.PhantomReference; import java.lang.ref.ReferenceQueue; public class Main { public static void main(String[]

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.