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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:18:09+00:00 2026-05-19T00:18:09+00:00

I have two threads thread1(printing numbers) & thread2(printing alphabets). My goal is to have

  • 0

I have two threads thread1(printing numbers) & thread2(printing alphabets).
My goal is to have the following output via syncronization:

1
a
2
b
3
c
4
d
5
e

class thread1 implements Runnable {
    public void run() {

        try {

            for (int i = 1; i <= 5; i++) {
                System.out.println("Is Thread1 holding lock of Testing.class?:"+Thread.holdsLock(Testing.class));
                synchronized (Testing.class) {
                    System.out.println("Is Thread1 holding lock of Testing.class?:"+Thread.holdsLock(Testing.class));

                    try {
                        System.out.println(i);
                        Testing.class.notifyAll();
                        System.out.println("Thread1:Going to wait");
                        Testing.class.wait();
                        System.out.println("Thread1:Resuming from wait");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }

            System.out.println("Finsihed thread1");


        } catch (Exception e) {
            System.out.println(e);
        }

    }
}



class thread2 implements Runnable {
    char[] alphabets = { 'a', 'b', 'c', 'd', 'e' };

    public void run() {

        try {

            for (int i = 0; i < 5; i++) {
                System.out.println("Is Thread2 holding lock of Testing.class?:"+Thread.holdsLock(Testing.class));
                synchronized (Testing.class) {
                    try {
                        System.out.println("Is Thread2 holding lock of Testing.class?:"+Thread.holdsLock(Testing.class));
                        System.out.println("Thread2:Going to wait");
                        Testing.class.wait();
                        System.out.println("Thread2:Resuming from wait");
                        System.out.println(alphabets[i]);
                        Testing.class.notifyAll();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }


        } catch (Exception e) {
            System.out.println(e);
        }

    }

}


    public class Testing {

        public static void main(String[] args) {
            Testing w= new Testing();
            thread1 t1 = new thread1();
            thread2 t2 = new thread2();
            Thread th1 = new Thread(t1, "");

            Thread th2 = new Thread(t2, "");
            try {
                th1.start();
                th2.start();
            } catch (Exception e) {
                System.out.println(e);
            }

        }
    }

I am getting the output:

Is Thread1 holding lock of
Testing.class?:false

Is Thread1 holding lock of
Testing.class?:true

1

Thread1:Going to wait

Is Thread2 holding lock of
Testing.class?:false

Is Thread2 holding lock of
Testing.class?:true

Thread2:Going to wait

How did thread2 got hold the lock of Testing.class when it was already locked by thread1?. Also, Is there any other elegant way to achieve this sync?. Thanks in advance.

  • 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-19T00:18:10+00:00Added an answer on May 19, 2026 at 12:18 am

    The basic problem you have is that you release the synchronized block allowing both threads to go through the loop at the same time. This means either thread can obtain the lock in any order.

    AFAIK, The simplest solution is to use a flag.

    final int length = 10;
    final AtomicBoolean flag = new AtomicBoolean();
    new Thread(new Runnable() {
        public void run() {
            for (int i=1;i<=length;i++) {
                while(flag.get());
                System.out.print(i+" ");
                flag.set(true);
            }
        }
    }).start();
    
    new Thread(new Runnable() {
        public void run() {
            for (char c='a';c<'a'+length;c++) {
                while(!flag.get());
                System.out.print(c+" ");
                flag.set(false);
            }
           System.out.println();
        }
    }).start();
    

    The most elegant way to have this task done in sync is to have one thread. Threads are specificly designed to execute two tasks with as little dependence as possible between them.

    BTW

    If notifyAll() is called before wait() is called, the notify will be lost. wait() will wait forever.

    You create a Testing object you discard (remove it).

    You trap InterruptedException which you print and continue as if nothing happened (i.e. remove the nested catch)

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

Sidebar

Related Questions

I have two threads and one class. Thread1 updates the local object of the
I wish to have two threads. The first thread1 occasionally calls the following pseudo
Suppose I have two threads(Thread1, Thread2) where the threads are accessing the cache for
I have some code that will be accessed from two threads: class Timer{ public:
I have two threads, one needs to poll a bunch of separate static resources
I have two threads, one updating an int and one reading it. This is
I have two threads in an Android application, one is the view thread, and
I need to have two way communication between threads in Tcl and all I
I have inherited a middle tier system with some multi-Threading issues. Two different threads,
I have two threads main thread and worker thread. What I want to be

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.