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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:41:10+00:00 2026-05-21T23:41:10+00:00

Can wait/notify be used within one thread? I’m mean I have a listener and

  • 0

Can wait/notify be used within one thread?
I’m mean I have a listener and in the moment when that listener gets called I wanna enable a thread to do his work.How could I do that?

UPDATE:My data is written in a database…and is written each time the listener is called.Now the thread that I’ve created reads that data and sends it somewhere….
Next…I get some other data and do the same thing….The other thread needs to know what was the last data he read it so he can start reading from where he left….

Take a look in here:
using wait and notify within one thread
This is how my problem looks like.Thx

I have the following:

synchronized (syncToken)
{
    try {
        syncToken.wait();

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
System.out.println("MyThread: " + s);

in MyThread….so when I do

MyThread t = new MyThread(syncToken);
t.start();

I put my thread on waiting…yes?

And when I do this:

syncToken.notify();

I get my thread back on track….but the execution of the next line is the one after wait()?

I mean this: System.out.println("MyThread: " + s); ????

When u notify a thred does he continues his execution with the line after wait()???Thx

  • 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-21T23:41:11+00:00Added an answer on May 21, 2026 at 11:41 pm

    The following is a simple example of concurrency between two different threads. In the example the main thread start a MyThread thread and every 3 seconds it sets a data to the MyThread instance and then MyThread prints it. The idea is to have a synchronized object that you wait on it and notify in the end of the usage to other threads that they can use it:

    Test.java:

    package stack;
    
    public class Test {
        public static void main (String args[])
        {
            Object syncToken = new Object();
            MyThread t = new MyThread(syncToken);
            t.start();
            for (int i = 0; i < 10; i++)
            {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized(syncToken)
                {
                    t.setText("Iteration " + i);
                    syncToken.notify();
                }
            }
        }
    }
    

    MyThread.java:

    package stack;
    
    public class MyThread extends Thread{
    
        String s;
        Object syncToken;
        public MyThread(Object syncToken)
        {
            this.s = "";
            this.syncToken = syncToken;
        }
    
        public void run()
        {
            while(true) // you will need to set some condition if you want to stop the thread in a certain time...
            {
                synchronized (syncToken)
                {
                    try {
                        syncToken.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("MyThread: " + s);
            }
        }
    
        public void setText(String s)
        {
            this.s = s;
        }
    }
    

    In this example, the main thread sets a string (every 3 seconds) and the MyThread thread prints it.

    Adapt it to your needs, it shouldn’t be too hard.

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

Sidebar

Related Questions

I have main thread which calls another thread. timeout period of second one is
what is the best way to stop a thread and wait for a statement
How can I wait the end of an ftp transfer on my iPhone app,
How can I wait for a hidden element to appear on the page? I
I'm successfully using yajl-objc along with ASIHTTPRequest in an iPhone project that does network
I am currently replacing some home baked task functionality with a new implementation using
I find myself having to get around waiting for AJAX in jQuery often these
i am using a MYSQL database as form of IPC datastore between web client
Our team has been testing our application with Selenium as it's heavily JavaScript driven
This sounds like a weird title and probably not stated too well. But here's

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.