Control the activity flow in thread T1 using thread T2

Hi, im asking this question because i have two threads, one running at background, one at foreground directly interacting with users. the two should also intereact with each other, and perform correct actions depending on the command/request received from the other. for instance,
Class BackgroundThread implements Runnable{
    private boolean _stop;
    public void run(){
        while(!shouldStop(){
            //do something
    public boolean shouldStop(){
        return _stop;
    public void signalStop(){
        _stop=false;
    public void signalContinue(){
        _stop=true;
Class ForegroundThread implements Runnable{
    BackgroundThread _bgThread;
    public ForegroundThread (BackgroundThread bgThread){
        _bgThread=bgThread;
    public void run(){
        // lines of code that catches user input from console
        case 'r'
            _bgThread.signalContinue();    //LINE A
            break;
        case 's'
            _bgThread.signalStop();   
            break;
public Class ClientMain(){
    public static void main(args[]){
        BackgroundThread bgT = new BackgroundThread();
        new Thread(bgT).start();
        new ForegroundThread (bgT).run;
    }these are just sample code, may have errors, but the logic i suppose is clear. basically users interact with foreground thread, and they can control the running of background thread by typing command, r and s. 's' signal stop, 'r' signal continue.
at present i managed to stop the background thread - strictly not stopping it, but stop goin into the 'while' loop - once user key in 's'; but then if i key in 'r', the program does nothing. while what i want the system to do is to resume the execution of 'while'. i tried to replace the line A to
_bgThread.signalContinue();
_bgThread.run();and it does re-run, but then i cannot control the thread no more. ie, it doesnt stop upon command 's' anymore.
how can i fix this please, any hints? thanks very much in advance!

when the background thread exits the while loop, the thread stops and exits.
try this, it works, but obviously will need to be customized for what you want to do. It should give you an idea though.
class BackgroundThread implements Runnable
    private boolean _stop;
    private boolean _wait = true;
    public void run(){
         while(_wait)
             while(!shouldStop()){
                  try{
                       Thread.sleep(1000);
                       System.out.println("Do Something");
                  catch(InterruptedException ie)
             try{
                  Thread.sleep(1000);
             catch(InterruptedException ie)
             System.out.println("Paused...");
    public boolean shouldStop(){
        return _stop;
    public void signalStop(){
        _stop=true;
    public void signalContinue(){
        _stop=false;
    public void signalPause()
         _stop = true;
    public void signalExit()
         _wait = false;
         _stop = true;
class ForegroundThread implements Runnable{
    BackgroundThread _bgThread;
    public ForegroundThread (BackgroundThread bgThread){
        _bgThread=bgThread;
    public void run(){
        // lines of code that catches user input from console
        Scanner scan = new Scanner(System.in);
        boolean exit = false;
         while(!exit)
             System.out.println("enter action");
             char resp = scan.next().charAt(0);
             switch(resp)
              case 'r':
                 _bgThread.signalContinue();    //LINE A
                 break;
             case 's':
                 _bgThread.signalPause();   
                 break;
             case 'e':
                  _bgThread.signalExit();
                  exit = true;
                  break;
public class ClientMain
    public static void main(String [] args )
        BackgroundThread bgT = new BackgroundThread();
        new Thread(bgT).start();
        new ForegroundThread(bgT).run();
}

Similar Messages

  • My business has 100 ipads 3g with no control at the moment - i need to now control the ipads - what apps they can use - and restict certian use on the ipads - whats the best way to do this - it will need to be done remotley.

    my business has 100 ipads 3g with no control at the moment - i need to now control the ipads - what apps they can use - and restict certian use on the ipads - whats the best way to do this - it will need to be done remotley.

    MDM  -- moble device management 
    MDMs
    Airwatch, Meraki, MobileIron, Profile Manager on OS X Server, or Zenprise
    For a comparison see this page:
    http://www.enterpriseios.com/wiki/Comparison_MDM_Providers
    Airwatch
      http://www.air-watch.com/
    Meraki -- A free MDM   [ expect lots of email and phone calls about upgrades ]
      http://www.meraki.com/products/systems-manager/
      https://discussions.apple.com/thread/4067210?tstart=0
      Good howto by Sergio Sosa.
      https://discussions.apple.com/thread/2594001?start=15&tstart=0
    MobileIron
      http://www.mobileiron.com/
    Profile Manager by Apple
    Zenprise
      http://www.zenprise.com/

  • Control the Batch Size for inseting tables using xi

    Hi Friends,
    Please let me know if we are able to control the batch size for inserting data using procedures for staging tables. Using SAP XI. Any blogs on this. or any information. please let us know.
    Thank you
    Regards
    Ramidi

    Hi Ramidi,
    There is something called message split on adapter engine but it won't guarantee a sequence of adapter calls.
    here is a blog about it:
    /people/jin.shin/blog/2006/02/07/multi-mapping-without-bpm--yes-it146s-possible
    Maybe there is also an another simple way to do it.
    Regards,
    Wojciech
    <i>** Please reward points if found it useful **</i>

  • How to control the number of running thread

    I need to maintain the number of running thread in the system or in a threadgroup, as too many running thread will make the system throughput drop tremendously because of the context swtich and memory problem. The threadgroup is not a solution as its active count is the number of the threads that are alive, it includes the thread being blocked as well. I don't know whether there is any way to control it without the use of an expensive monitor?
    Thanks for any help

    If you create a pool of any sort you will need a point where all requests for a resource from the pool must go via, and that piece of code could potentially become a bottle neck. But if you develop it well there will be a very small piece of code that is sychrnonized that will either put a request in a queue as there are no resources available, or it will provide the requested resource. As far as i am aware there is no safe way around this as you will have multiple threads requesting the resource.

  • How can I control the FAQ title with CSS when using the Ajax effect

    I am calling the FAQ module with the Ajax effect.
    I would like to set different states with CSS to the title;
    I have tried to use :hover, :active and :focus on the div.
    It would be nice with a different styling when the item is "open"
    (I know there is a problem with the stacking, but I posted that in another thread)
    link

    Hi
    There is one thing You might miss in iDVD - So do I - the ability to re-arrange in the DVD map (block diagram)
    The order things will be pplayed or addrssed is same as the order each item is introduced into iDVD.
    To my knowledge ther is no way around this.
    Yours Bengt W

  • How can I control the order of iDVD menu items using the Arrow keys?

    I have a submenu screen containing 10 selectable buttons plus the Return Arrow button.  I have ordered all the slideshows in the correct order in the DVD map.  However, when I run the program, the arrow keys jump the selection focus in an apparently random sequence.  How can I control the order of the selection focus?
    I'm using iDVD v 7.1.2
    TIA.
    Ron

    Hi
    There is one thing You might miss in iDVD - So do I - the ability to re-arrange in the DVD map (block diagram)
    The order things will be pplayed or addrssed is same as the order each item is introduced into iDVD.
    To my knowledge ther is no way around this.
    Yours Bengt W

  • How can i control the motor movement of stepper motor using a NI PXI 7330

    Good day to all,
    i'm a newbie in programming in motion control of NI. I just want to ask if how can i control the clockwise and counter-clockwise rotation of a stepper motor using NI PXI 7330 module. all i can do is the counter-clockwise rotation using the example vi (Simple one-axis move.vi) found in the examples of LabView 2009.
    Thanks

    what type of limit switch sensor are you using and how did you wire itß What are your MAX settings regarding the limit switches?
    Good day SIr Jochen,
    I am using two reed switch for the reverse and forward limit. I wired the signal wire of the reed switch to the limit ports(forward and reverse Limit) and its ground to the digital ground of the UMI-7764.
    the attachment below is the my max configuration about limit switches
    Attachments:
    max configuration.JPG ‏155 KB

  • Controlling the sequence of an iPod slideshow using Elements

    I'm trying to control the order in which photos are shown in an iPod slideshow (that I'm displaying on a TV screen)
    I've been asking how to do this in some iPod forums, and someone suggested (without many specifics) that I could somehow set it up in Elements and then export it to the iPod.
    If you know how to do this or can point me to a post, article, etc that gives an explanation, I would really appreciate it! Otherwise, it's all trial and error.

    Doesn't it just show files in filename order?

  • How can we control the executions of 4 threads

    Hi,
    I tried to create 4 threads and tried to run them in custimized manner but i m facing some problem please help me in thi regard.Below is the code which i wrote to schedule the threads in custimized may using wait and notify...
    public class MyThreadOne implements Runnable {
    String tName;
    Thread t;
    MyThreadOne(String threadName) {
    tName = threadName;
    t = new Thread(this, tName);
    t.start();
    public void run() {
              // wait for me to be notified by main thread before continuing.
              synchronized (t) {
                        try {
                        wait();
                        } catch(InterruptedException e) {
                        throw new RuntimeException(e);
              // after main thread notifies we run normally.
    System.out.println("Thread: " + tName);
    System.out.println("Terminating thread: " + tName);
    public Thread getThread() {
    return this.t;
    public static void main(String args[]) {
    // Why are the threads start and terminate randomly?
    MyThreadOne t1 = new MyThreadOne("1");
         MyThreadOne t2 = new MyThreadOne("2");
         MyThreadOne t3 = new MyThreadOne("3");
         MyThreadOne t4 = new MyThreadOne("4");
         // issue a notify to each thread to have them start up
         System.out.println(" notify");          //phani
                   synchronized(t1.getThread()) {
                                  System.out.println("synchronized ");          //phani
                   t1.getThread().notify();
                   // optional: Thread.sleep(50) here if we want to try to give some lead time...
                   synchronized(t2.getThread()) {
                   t2.getThread().notify();
                   synchronized(t3.getThread()) {
                   t3.getThread().notify();
                   synchronized(t4.getThread()) {
                   t4.getThread().notify();
         try {
         //NO -> Thread.sleep(10000);
         // now wait for each thread to exit.
         t1.getThread().join();
         t2.getThread().join();
         t3.getThread().join();
         t4.getThread().join();
         // we could also do t1.isAlive();
         } catch (InterruptedException e) {
         System.out.println(
         "Exception: Thread main interrupted.");
         System.out.println(
         "Terminating thread: main thread.");
    Edited by: user13779505 on Jan 18, 2011 3:39 AM
    Edited by: user13779505 on Jan 18, 2011 9:23 PM
    Edited by: user13779505 on Jan 19, 2011 11:39 PM

    user13779505 wrote:
    if there is some correction in my program please correct itThats hard to do as I assume you doing this as a learning exercise, meaning you have make the changes.
    I have already said the best way is to use a loop.
    When you get the code working it should do the same thing as the following. Why would you make it more complicated?
    for(int tName=1;tName<=4;tname++)
        System.out.println("Thread: " + tName + "\nTerminating thread: " + tName);
    System.out.println("Terminating thread: main thread.");

  • Monitoring and controlling the Tag values of Logix 5000 using Labview while using RSLogix 5000 to program the PLC

    Hi
      I want to know how to change the input bits or tags of a PLC which is programmed using RSlogix 5000. But I need to use Labview to monitor the input bits ( like in Digitial Input bits) and then be able to change the BITS while the PLC runs and the PLC should be able to detect the change in input bits and act accordingly. The programming is done using ladder logic and its done in RSLogix 5000 and dumped into PLC using RSLogix.
    Thanks in advance

    msu,
    Once you have the OPC server up and running the best way to communicate with is is through the Datalogging and Supervisory Control module for LabVIEW.  The DSC module allows you to bind shared variables to items on your OPC server.  Then you could just read from or write to these shared variables to access the data on this OPC server.  If you don't want to get this module you could also just use datasocket to read/write from an OPC server.  The URL you would use in data socket would look something like "opc://localhost/{your opc item's address}".
    -Justin D

  • The active browser window that I use is constantly de-activating and I have to click on the window in order to navigate, type or do anything. It behaves as if I have clicked on another window to use even if it is the only window I have open.

    Seems as if there is a set amount of time I have to use a browser window and when that time is up, I have to click on the window to use it again. Very aggravating. May not be a firefox issue, but I can't pin it down.

    Hi username availabl,
    Thanks for visiting Apple Support Communities.
    If windows and menus on your computer are unexpectedly "losing focus" every few seconds, there are a few steps that can help isolate or resolve the issue.
    First, try booting your computer into safe mode:
    Mac OS X: What is Safe Boot, Safe Mode?
    http://support.apple.com/kb/ht1564
    If things are working normally in safe mode, this usually means OS X is fine, and the issue is caused by conflicting software in your user account.
    Next, see if the issue occurs in a new user account. The steps in this article also apply to creating a new user in Mavericks:
    OS X Mountain Lion: Create a new user account
    http://support.apple.com/kb/PH11468
    If the issue persists in safe mode and a new user, I would recommend reinstalling Mavericks:
    OS X Mavericks: Reinstall OS X
    http://support.apple.com/kb/PH13871
    Best Regards,
    Jeremy

  • How much is the activation fee when you upgrade using "New Every Two?"

    i went to walmart to do an upgrade,we havent had a new phone in many yrs,they charge u 30 dollars for ativation,i called verizon they said they started that 2 months ago,i been with verizon for many,many yrs,i just use to do *228 n push option 1 oh u still can if u buy a phone from a friend or wastnot,but any new phone 30 $ but u call loyalty they waive 20 of it,i still dont think that is right!!!!

    Verizon took all the change in my wallet!

  • How does one control the appearance of a web browser using Flash Builder?

    Using Flash Builder I wish to record the existing appearance of a web browser (e.g. position, size, menubar, caption bar, status bar) and change it to full screen, no caption, no menubar, no status bar, etc.  In this mode I will load a full screen graphic and other content in the browser and I want the user to not realize that everything is being displayed in a browser.  Later I need to return it to the initial appearance using the recorded settings.  Any guidance is appreciated.

    It appears that one must launch a new a new browser using JavaScript from Flash Builder whereby many, but not all of the properties I detailed, can be set.  Many browsers disable or can be configured to disable JavaScript from setting some of the properties.  Launching a new window in the 1st place is disabled in many browsers.

  • Control the Post Date  when use BAPI_OUTB_DELIVERY_CONFIRM_DEC to PGI !

    Dear all,
       How can I control the Post Date (WADAT_IST) when I use BAPI_OUTB_DELIVERY_CONFIRM_DEC to PGI?
    Thanks to help me.

    In ECC 6.0, I used this BAPI to perform the picking using this way:
    1. Implement enhancement point  BAPI_OUTB_DELIVERY_CONFIRM_D01 in enhancemnt spot ES_SAPLV50I to set the update picking flag on delivery control structure.
    GS_DLV_BAPI_CONTROL-UPDATE_PICKING = 'X'
    2. Implement enhancement of standard BADI SMOD_V50B0001. Use method EXIT_SAPLV50I_004 to loop at table CT_VBPOK and update the fields VBELN and POSNN with suitable values.
    There is one problem with this solution, though.  The delivery quantity cannot be updated.  Any increase in the delivery qty ITEM_DATA-DLV_QTY actually updates the picked quantity on the delivery.  Consequently, there is little practical use of this technique.
    John

  • I need to program a Hittite Fractional​-N Synthesize​r Evaluation Kit with a HMC702LP6C​E an external YIG oscillator​. Not sure how to use Labview to control the PLL.

    I need to program a Hittite Fractional-N Synthesizer Evaluation Kit with a HMC702LP6CE an external YIG oscillator.  Not sure how to use Labview to control the PLL.

    Here is how to use the PLL. But I don't know of how to interact with that device
    http://zone.ni.com/devzone/cda/tut/p/id/3781
    And for thouse who don't know what a PLL is a free bonus link is here:
    http://digital.ni.com/public.nsf/allkb/07BC8D77D4E​9AE258625708B007CE74F?OpenDocument
    and a second one on what that device is: http://www.hittite.com/products/view.html/view/HMC​702LP6CE
    Now we are all caught up to speed.
    Sam S
    Applications Engineer
    National Instruments

Maybe you are looking for

  • Other than the -36.....

    I'm getting the following message; "A buffer overrun has been detected which has corrupted the programs internal state. The program cannot safely continue execution and must now be terminated." I hit "OK" and the entire QT program shuts down. Scripts

  • [SOLVED] Laptop screen blank (black)

    Hey there I left my laptop (dell vostro 1520) for about half an hour running on my desk. After coming back, the screen was black and could not be turned on again. The screen is still black after several reboots. I cannot even see the bios loading scr

  • Advance Grid using Remote Object

    Hai All, Kindly check this code advance grid is not filling.But the datas are coming .Please help me      private function init():void                 ProductDataService = new RemoteObject("GenericDestination");                 ProductDataService.sou

  • Hyperlinks in a .pdf

    Can someone telll me how can I edit a hyperlink in a .pdf file?

  • HOW I CAN USE JAVASCRIPT IN 11GFORMS

    ANY EXAMPLE / TUTORIAL / SAMPLE?