Threads and blocking i/o

background i am building a multi-cliented server based on the model of one thread per client. client threads wait on a BufferedReader in the run() method for data over the network and take the appropriate action based on the input.
problem a client thread needs in certains circumstances to kill the thread of another client. since the run() method loops on the blocking input of BufferedReader, any flag or call to interrupt() will not be evaluated until the BufferedReader unblocks. this is bad since it means that the client who is to be killed will not return from run() until the client app talking to the thread sends some data to unblock BufferedReader. the result is a potential dos attack against this server. bad stuff.
example the current situation: client app one connects to the server spawning Thread-1. client app two connects to the server spawning Thread-2. app two triggers killSomeClient() on Thread-1. Thread-1 continues to linger until client app 1 transmits data to the server, whereupon Thread-1 dies.
example the required functionality: app one connects to Thread-1. app two connects to Thread-2. app two triggers killSomeClient() on Thread-1. Thread-1 returns from the run() loop and the thread dies immediately.
what i've tried well, obviously, stop() and destroy() are what i really want... but that's not an option. have tried setting a flag in the while loop to be set by killSomeClient() (ie while(keepRunning && (inputFromClient = in.readLine()) != null) ). have tried interrupt() in the same manner.
potential solutions i can think of a replacement for stop() or some effective non-blocking read allowing me to loop around the reader, grabbing one char at a time and continuing the loop even if the queue is empty.
the code example this is a very pared-down version of the code showing only the material necessary for this example (better for readability!). aside from this issue, the application runs.
someClient
public class someClient{
    private Socket socket;
    private BufferedReader in;
    private String inputFromClient;
    someClient(Socket socket){
        this.socket = socket;
        this.in = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
    public void run(){
        try{
            // waits on blocking input
            while((inputFromClient = in.readLine()) != null){
                System.out.println(inputFromClient);
        }catch(Exception e){}
    public void killSomeClient(someClient killme){
        // help!!
someServer
public class someServer{
    private ServerSocket ss;
    public someServer(){
        ss = new ServerSocket(9999);
    public startServer{
        Socket socket = ss.accept();
        someClient client = new someClient(socket);
        client.start();
}

problem a client thread needs in certains
circumstances to kill the thread of another client.
since the run() method loops on the blocking input of
BufferedReader, any flag or call to interrupt() will
not be evaluated until the BufferedReader unblocks.In general, the best approach to terminating a blocking operation is to invalidate the object on which that operation depends. In your case, closing the socket will terminate the read().
The following is a rather lengthy piece of code that's actually been tested :-)
import java.io.*;
import java.net.*;
public class SocketStopper
    private final static int  PORTNUM = 10000;
    public static void main( String argv[] )
    throws Exception
        System.err.println("Starting server");
        Server server = new Server();
        (new Thread(server)).start();
        Thread.sleep(2000);
        System.err.println("Starting client");
        Client client = new Client();
        (new Thread(client)).start();
        Thread.sleep(2000);
        System.err.println("About to close server socket");
        server.closeSocket();
        Thread.sleep(2000);
        System.err.println("About to exit");
        System.exit(0);
    /** This object establishes a connection to the server port and then
     *  sleeps for 10 minutes without writing anything. This should cause
     *  the reader to block for input.
    public static class Client
    implements Runnable
        public void run()
            try
                Socket sock = new Socket(InetAddress.getLocalHost(), PORTNUM);
                System.err.println("Client established connection");
            catch (Exception e)
                System.err.println("Client unable to establish connection: " + e);
                return;
            try
                Thread.sleep(10 * 60000L);
            catch (InterruptedException ignored)
    /** This object sets up a server socket. It accepts the first connection
     *  from that socket and attempts to read from it, which should block.
    public static class Server
    implements Runnable
        private Socket _sock;
        public void run()
            try
                ServerSocket srv = new ServerSocket(PORTNUM);
                _sock = srv.accept();
                System.err.println("Server accepted connection");
            catch (Exception e)
                System.err.println("Server unable to receive connection: " + e);
                return;
            try
                _sock.getInputStream().read();
                System.err.println("read() completed normally");
            catch (IOException e)
                System.err.println("read() terminated with exception: " + e);
        public void closeSocket()
            try
                _sock.close();
            catch (IOException ignored)
}

Similar Messages

  • Question About Java Threads and Blocking

    I'm helping someone rehost a tool from the PC to the Sun. We're using the Netbeans IDE and the Java programming language. I took a Java course several years ago, but need some help with something now. We're developing a front-end GUI using Swing to allow users to select different options to perform their tasks. I have a general question that will apply to all cases where we run an external process from the GUI. We have a "CommandProcessor" class that will call an external process using the "ProcessBuilder" class. I'm including the snippet of code below where this happens. We pass in a string which is the command we want to run. We also instantiate a class called "StreamGobbler" my coworker got off the Internet for redirecting I/O to a message window. I'm also including the "StreamGobbler" class below for reference. Here's the "CommandProcessor" class:
    // Test ProcessBuilder
    public class CommandProcessor {
    public static void Run(String[] cmd) throws Exception {
    System.out.println("inside CommandProcessor.Run function...");
    Process p = new ProcessBuilder(cmd).start();
    StreamGobbler s1 = new StreamGobbler("stdin", p.getInputStream());
    StreamGobbler s2 = new StreamGobbler("stderr", p.getErrorStream());
    s1.start();
    s2.start();
    //p.waitFor();
    System.out.println("Process Returned");
    Here's the "StreamGobbler" class:
    import java.lang.*;
    import java.io.*;
    // Attempt to make the output of the process go to the message window
    // as it is produced rather that waiting for the process to finish
    public class StreamGobbler implements Runnable {
    String name;
    InputStream is;
    Thread thread;
    public StreamGobbler (String name, InputStream is){
    this.name = name;
    this.is = is;
    public void start(){
    thread = new Thread (this);
    thread.start();
    public void run(){
    try{
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    while (true){
    String s = br.readLine();
    if (s == null) break;
    System.out.println(s);
    //messageWindow.writeToMessageArea("[" + name + "]" + s);
    is.close();
    catch(Exception ex){
    System.out.println("Problem reading stream" + name + "...:" + ex);
    ex.printStackTrace();
    The "CommandProcessor" class calls two (2) instances of the "StreamGobbler" class, one for "stdin" and one for "stderr". My coworker discovered these are the 2 I/O descriptors that are needed for the external command we're running in this case. We're actually called the Concurrent Versions System (cvs) command from the GUI. Here's what we need it to do:
    We want to display the output real-time as the external process is executing, but we want to block any actions being performed on the GUI itself until the process finishes. In other words, we want to show the user any generated output from the process, but don't want to alllow them to perform any other actions on the GUI until this process has finished. If we use the "waitFor()" function associated with a process, it blocks all external process output until the process has completed and then spews all the output to the screen all at once. That's NOT what we want. Also, if we don't use the "waitFor()" function, the code just continues on as it should, but we don't know how to block any actions on the GUI until this process has finished. My coworker tried the following code, but it also blocked any output until the process had finished:
    while (s1.thread.isAlive() || s2.thread.isAlive())
    // We really don't do anything here
    I'm pretty sure we have to use threads for the output, but how do we instantly show all output and block any GUI actions?
    Thank you in advance for your help!

    You're talking about a GUI, but there's nothing in that code which is putting events into the GUI update thread. You also say that nothing happens to the GUI until the CommandProcessor.Run() method returns if you wait for the process.
    This implies that you're calling CommandProcessor.Run() in an ActionListener. This will block the GUI thread until it completes.
    I was going to explain what to do, but a quick Google informed me that there's a new class which is designed to help in these situations SwingWorker (or as a [separate library|https://swingworker.dev.java.net/] if you're not up-to-date yet).

  • Problem with threads and ProgressMonitor

    Dear Friends:
    I have a little problem with a thread and a ProgressMonitor. I have a long time process that runs in a thread (the thread is in an separate class). The thread has a ProgressMonitor that works fine and shows the tasks progress.
    But I need deactivate the main class(the main class is the user interface) until the thread ends.
    I use something like this:
    LongTask myTask=new LongTask();
    myTask.start();
    myTask.join();
    Now, the main class waits for the task to end, but the progress monitor don`t works fine: it shows only the dialog but not the progress bar.
    What's wrong?

    Is the dialog a modal dialog? This can block other UI updates.
    In general, you should make sure that it isn't modal, and that your workThread has a fairly low priority so that the UI can do its updating

  • Having a problem with threads and using locks

    I was hoping someone could give me some hits on getting my code to run properly. The problem I am having is I think my locks and unlocks are not working properly. Plus, for some reason I always get the same output, which is:
    Withdrawal Threads         Deposit Threads            Balance
    Thread 2 attempts $29 Withdrawal - Blocked - Insufficient Funds
    Thread 4 attempts $45 Withdrawal - Blocked - Insufficient Funds
    Thread 6 attempts $34 Withdrawal - Blocked - Insufficient Funds
    Thread 7 attempts $40 Withdrawal - Blocked - Insufficient Funds
                                    Thread 1 deposits $187          $187
                                    Thread 3 deposits $169          $356
                                    Thread 5 deposits $61           $417
    Press any key to continue...If someone can see the error I made and doesn't mind explaining it to me, so I can learn from it, I would appreciate that very much.
    /************Assign2_Main.java************/
    import java.util.concurrent.*;
    public class Assign2_Main
    {//start Assign2_Main
        public static void main(String[] args)
        {//start main
               // create ExecutorService to manage threads
               ExecutorService threadExecutor = Executors.newCachedThreadPool();
            Account account = new SynchronizedThreads();
            Deposit deposit1 = new Deposit(account, "Thread 1");
            Deposit deposit2 = new Deposit(account, "Thread 3");
            Deposit deposit3 = new Deposit(account, "Thread 5");
            Withdrawal withdrawal1 = new Withdrawal(account, "Thread 2");
            Withdrawal withdrawal2 = new Withdrawal(account, "Thread 4");
            Withdrawal withdrawal3 = new Withdrawal(account, "Thread 6");
            Withdrawal withdrawal4 = new Withdrawal(account, "Thread 7");
            System.out.println("Withdrawal Threads\t\tDeposit Threads\t\t\tBalance");
            System.out.println("------------------\t\t---------------\t\t\t-------\n");
            try
                threadExecutor.execute(withdrawal1);       
                threadExecutor.execute(deposit1);     
                threadExecutor.execute(withdrawal2);  
                threadExecutor.execute(deposit2);    
                threadExecutor.execute(withdrawal3);
                threadExecutor.execute(deposit3);       
                threadExecutor.execute(withdrawal4);
            catch ( Exception e )
                 e.printStackTrace();
                //shutdown worker threads
               threadExecutor.shutdown();
        }//end main
    }//end Assign2_Main/******************Withdrawal.java****************************/
    public class Withdrawal implements Runnable
    {//start  class Withdrawal
          *constructor
        public Withdrawal(Account money, String n)
             account = money;
             name = n;
        public void run()
        {//start ruin
             int newNum = 0;
                newNum = account.getBalance(name); 
               Thread.yield();
        }//end run
        private Account account;
        private String name;
    }//end  class Withdrawal/*******************Deposit.java***************/
    import java.util.Random;
    public class Deposit implements Runnable
    {//start class Deposit
          *constructor
        public Deposit(Account money, String n)
             account = money;
             name = n;
        public void run()
        {//start run
                try
                     Thread.sleep(100);
                   account.setBalance(random.nextInt(200), name);
                }// end try
                catch (InterruptedException e)
                  e.printStackTrace();
       }//end run
       private Account account;
       private Random random = new Random();
       private String name;
    }//end class Deposit/********************Account.java*****************/
    *Account interface specifies methods called by Producer and Consumer.
    public interface Account
         //place sum into Account
         public void setBalance(int sum, String name);
         //return  value of Account
            public int getBalance(String name);         
    } /**************SynchronizedThreads.java****************/
    import java.util.concurrent.locks.*;
    import java.util.Random;
    public class SynchronizedThreads implements Account
    {//start SynchronizedThreads
          *place money into buffer
        public void setBalance(int amount, String name)
        {//start setBalance
             // lock object
             myLock.lock();           
            sum += amount;
            System.out.println("\t\t\t\t" + name + " deposits $" + amount +"\t\t$"+ sum+"\n");       
               //threads are singnaled to wakeup
            MakeWD.signalAll();
              // unlock object                                                
            myLock.unlock();
           }//end setBalance
            *gets the balance from buffer
           public int getBalance(String name)
        {//start getBalance
             int NewSum = random.nextInt(50);
             //lock object
            myLock.lock();
            try
                 if(sum > NewSum)
                     //takes NewSum away from the account
                     sum -= NewSum;
                        System.out.println(name + " withdraws $" + NewSum +"\t\t\t\t\t\t$"+ sum+"\n");
                else
                     System.out.println(name +  " attempts $" + NewSum + " Withdrawal - Blocked - Insufficient Funds\n");                 
                     //not enough funds so thread waits
                        MakeWD.await();         
                //threads are singnaled to wakeup
                MakeD.signalAll();     
                }//end try
            catch (InterruptedException e)
                   e.printStackTrace();
            finally
                 //unlock object
                 myLock.unlock();
            return NewSum;     
         }//end getBalance
         private Random random = new Random();  
         private Lock myLock = new ReentrantLock();
         private Condition MakeD = myLock.newCondition();
         private Condition MakeWD = myLock.newCondition();
         private int sum = 0;
    }//end SynchronizedThreads

    You can also try to provide a greater Priority to your player thread so that it gains the CPU time when ever it needs it and don't harm the playback.
    However a loop in a Thread and that to an infinite loop is one kind of very bad programming, 'cuz the loop eats up most of your CPU time which in turn adds up more delays of the execution of other tasks (just as in your case it is the playback). By witting codes bit efficiently and planning out the architectural execution flow of the app before start writing the code helps solve these kind of issues.
    You can go through [this simple tutorial|http://oreilly.com/catalog/expjava/excerpt/index.html] about Basics of Java and Threads to know more about threads.
    Regds,
    SD
    N.B. And yes there are more articles and tutorials available but much of them targets the Java SE / EE, but if you want to read them here is [another great one straight from SUN|http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html] .
    Edited by: find_suvro@SDN on 7 Nov, 2008 12:00 PM

  • Little bit of thread and little bit of instant messenger design problem

    OK! i have a small problem here, i am planning on making a instant messenger server and client, not compatible with any out there(as of yet) and not using any stansard protocols out there, just something i made on my own. ok now the problem i am trying to figure out is....i have a "Messaging Server" as the name suggests it sends messages from one client to the other, how it is setup is, client connects to this server, the server accepts and creates a new thread for this client and adds a message listener to this client handler thread. as and when the server creates this thread for the client, it puts it in a vector which is keeping track of every client connected to this server. so now when the client sends a message to another client, basically it goes through this server, the thread upon receiving this message, fires a method of the listener. this method takes the message, finds out who it is going to(therefore the userid) and seaches the threads vector to find the appropriate thread and then just creats another striped down message out of the received message and calls a method in that thread to send this newly created message and hence the message should be sent.(i am speculating, i still have not created this)
    so the problem ia m logically facing is...at what point of time who is taking the load, what i mean by that is, when the thread receives the message, ofcourse it is this thread which is working is taking the load, but when the listener fucntion is fired which thread is handlign the call is it the client handler thread which fired the method or the messaging server thread which creates these threads?? now once that is solved, who(which thread) is doing the work when i call the fucntion in the other thread which i found aftre searching the vector. i mean i am thinking like this cuz i am not much in sink with the way threads work. i am also trying to figure out this to find out the load the server or the threads will be having.
    algo example:
    class messageServer
         vector currentWorkingThreads;
         messageListener listener;
         main()
              listener = new messageListener(); //should it be here or the other place i am mentioning below
              while(true)
                   clientHandler = socket.accept();
                   listener = new messageListener(); //should it be here or the other place i am mentioning above
                   //create a thread and add this thread object to the vector
                   thread.addLisenter(listener);
    class messageServerThread
         string userid;
         messageListener lis;
         run()
              object obj = in.readObject();
              lis.sendMessage(obj);
         sendMessage(obj)
              out.writeObject(obj);
              out.flush;
    class messageListener
         sendMessage(msSendMessage msg)
              //find the appropriate user thread
              loop(currentWorkingThread.next())
                   messageServerThread msgThread = (messageServerThread)currentWorkingThread.element();
                   if(msg.userid == msgThread.userid)
                        msReceiveMessage rcvMsg = msg.strip(); //dosent matter what this does, it just strips the unwanted info and returns the type msReceiveMessage
                        msgThread.sendMessage(rcvMsg);
    and if this is something out of the line, or not a good way of doing it, can someone help me with the design it would be gr8.
    thanx a lot
    -Ankur
    help would be greatly appreciated.
    [email protected]

    other, how it is setup is, client connects to this
    server, the server accepts and creates a new thread
    for this client and adds a message listener to this
    client handler thread. as and when the server creates
    this thread for the client, it puts it in a vector
    which is keeping track of every client connected to
    this server.ok so far.
    so now when the client sends a message to
    another client, basically it goes through this server,
    the thread upon receiving this message, fires a method
    of the listener. this method takes the message, finds
    out who it is going to(therefore the userid) and
    seaches the threads vector to find the appropriate
    thread and then just creats another striped down
    message out of the received message and calls a method
    in that thread to send this newly created message andhere you seem to confuse the thread instance and the thread of execution. the thread invoking this method is still the event handling thread (i.e. the receiver thread of the socket). you do not need threads for delivering - this can be done by the receiving sockets' event handling threads. disadvantage: if processing of the message takes significant time then the socket is blocked and cannot process another message.
    solution: put the message in an event queue which is processed by one or more threads. (see my example i e-mailed you)
    robert
    hence the message should be sent.(i am speculating, i
    still have not created this)
    so the problem ia m logically facing is...at what
    point of time who is taking the load, what i mean by
    that is, when the thread receives the message,
    ofcourse it is this thread which is working is taking
    the load, but when the listener fucntion is fired
    which thread is handlign the call is it the client
    handler thread which fired the method or the messaging
    server thread which creates these threads?? now once
    that is solved, who(which thread) is doing the work
    when i call the fucntion in the other thread which i
    found aftre searching the vector. i mean i am thinking
    like this cuz i am not much in sink with the way
    threads work. i am also trying to figure out this to
    find out the load the server or the threads will be
    having.
    algo example:
    class messageServer
    vector currentWorkingThreads;
    messageListener listener;
    main()
    listener = new messageListener(); //should it be
    e here or the other place i am mentioning below
    while(true)
    clientHandler = socket.accept();
    listener = new messageListener(); //should it be
    be here or the other place i am mentioning above
    //create a thread and add this thread object to the
    he vector
    thread.addLisenter(listener);
    class messageServerThread
    string userid;
    messageListener lis;
    run()
    object obj = in.readObject();
    lis.sendMessage(obj);
    sendMessage(obj)
    out.writeObject(obj);
    out.flush;
    class messageListener
    sendMessage(msSendMessage msg)
    //find the appropriate user thread
    loop(currentWorkingThread.next())
    messageServerThread msgThread =
    =
    (messageServerThread)currentWorkingThread.element();
    if(msg.userid == msgThread.userid)
    msReceiveMessage rcvMsg = msg.strip(); //dosent
    ent matter what this does, it just strips the unwanted
    info and returns the type msReceiveMessage
    msgThread.sendMessage(rcvMsg);
    and if this is something out of the line, or not a
    good way of doing it, can someone help me with the
    design it would be gr8.
    thanx a lot
    -Ankur
    help would be greatly appreciated.
    [email protected]

  • Should I use a thread and (in this case) how?

    Hi:
    I have a Swing frame (frameA) and on which there is a button; if I double click it,
    it will bring up a second frame (frameB). On frameB, I will select a new button name
    from a combobox, and click "DONE" button, and frameB would change the name of the
    button on frameA to this new name I selected. And frameB would disappear as well.
    I am thinking of using another thread for painting frameB, because I don't know how long
    it would take for frameB to load up, and I don't want to block up the event-dispatching thread
    on frameA.
    However, since frameB uses another thread, and frameB also has an ActionListener
    associated with the DONE button, i am not sure how to make the thread stay alive
    so that it could check whether the ActionEvent is fired. So, in this case, is it necessary to use
    another thread to do the job? Or should I just use the same event-dispatching thread?
    Or is there another whole new approach much better?
    Thanks

    Quite simply, I wouldn't bother threading it.
    If you load, link and instantiate the pop-up frame (actually I'd use a Dialog here) before you trigger the pop-up, the reaction is more or less instant. The execution of th parent frame will continue after the pop-up has been made visible, so any way you cut it, you are going to get a brief pause in the parent UI while the pop-up is shown - you may as well have it when the button is clicked.
    Hope this helps.--
    <sig> http://www.itswalky.com http://www.crfh.net </sig>

  • Threading and Re-Use of buffers Using Call By Reference node (Duct Tape required)

    I have been trying to get the following information into the public domain for years and now that I have the answers, I will share with those that may be interested.
    Warning!
    Wrap your head in duct tape before reading just for safety sakes.
    My two questions have been;
    1) can LV Re-use the buffers of the calling VI when using VI Serve Call by reference?
    2) Is the UI thread used when using Call by reference?
    1. When calling a VI using the call by reference node, does the data going into the connector pane of the node get copied, or is it in-line as it would be with a properly set up subVI?
    Short answer: It is somewhere in-between.
    Long answer:
    The compiler doesn't know what VI will be called, but it does have a hint:
    the reference wired into the Call By Reference node. It uses that to get the "Prototype" for the call. So for the best performance, use a prototype that has the same "in-placeness characteristics" as the called VI. That being said, users don't know what the "in-placeness characteristics" are.
    Before I go into the details, I should say that the overhead of these copies shouldn't matter much unless it is a large data structure (an array with a lot of elements, or a cluster/class with many fields or containing large arrays etc.).
    Example 1:
    If the prototype does not modify the data then the compiler assumes that the Call By Reference node will not modify the data. However at run-time a check is made to see if the actual called VI will modify the data. If so, then a copy is made and passed in so that the original data can remain unmodified.
    Example 2:
    If the prototype contains an input that is wired through to an output in such a way that both the input and output terminals can use the same memory buffer, but at run-time a check determines that the actual called VI's input and output do not share a buffer, then a copy will be made from the actual call's output to the original VIs (combined input and output) buffer.
    I should also mention that even with this "attempt to agree with the prototype" behavior, it's not always possible to get as good performance as a regular SubVI call. For instance if you have a situation where the prototype does not modify the data and passes it through to an output then the compiler must assume that the data is modified (because as in example 2, there exist VIs that may modify it even if the actual VI called does not).
    And there are some caveats:
    1) This "using a prototype" behavior was new to 2009. Before that we used a more naive way of passing data that assumed all inputs will be modified and no outputs share a buffer with an input.
    2) This behavior is subject to change in future versions, if we find further optimizations.
    3) This behavior is the same as we use for dynamic dispatch VIs (when using LV classes)
    4) If you want to create a VI only to be used as a prototype, then you can use features of the In-Place Element Structure to control the "in-placeness characteristics" Namely the In/Out Element border nodes, the "Mark as modifier" feature of the border nodes (note the pencil icon on the In Element), and the Always Copy node.
    5) The prototype is only the first reference ever wired into the Call By Reference node. So if you do make a new prototype VI, you can't just make a reference out of it to wire to the Call By Reference node. I suggest deleting the Call By Reference node and dropping a new one.
    6) For remote calls, we always have to "make copies" by passing data over a network.
    I hope this helps, if you want any further information/clarification, then feel free to ask.
    2. Does the call by reference node execute in the UI thread? If the call is being made by a remote machine over ethernet, which thread does the host (the machine that makes the call by reference) execute on and which thread does the target (the machine that contains the VI file) execute on?
    In the local case, the Call by Reference node does not require the UI thread and can run in whatever thread the VI wants to execute in.
    When calling a remote VI, the Call by Reference node uses the UI thread (detailed below) on both the client and on the server.
    The client uses the UI thread to send the call request to the server and then again when the response comes back. The UI thread is not blocked during the time in between.
    The server receives the TCP message in the UI thread and then starts the call in the UI thread. The server also uses the UI thread to send the response back to the client. The UI thread is not blocked on the server during the execution of the VI.
    I hope people find this when they need it!
    Ben
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction
    Solved!
    Go to Solution.

    I never use duct tape. I wrap my head in aluminum foil and thus get much better shielding from the aliens trying to tap my mind.
    Also easier to remove later, but why risk taking it off??
    LabVIEW Champion . Do more with less code and in less time .

  • Testcase for error - "Unable to allocate thread control block"

    Hello,
    The down-below code throws the error message:
    db_stat: Unable to allocate thread control block
    BDB: 4.8, OS: Ubuntu 9.10
    Thanks
    #include <stdlib.h>
    #include <sys/ipc.h>
    #include <db.h>
    #define DIRECTORY "/tmp/db"
    int main()
    // open environment
    DB_ENV *handle;
    int status = db_env_create( &handle, 0 );
    status = handle->set_cachesize( handle, 0, 32*1024*1024, 0 );
    status = handle->set_thread_count( handle, 0x100 );
    key_t key = ftok( DIRECTORY, 'M' );
    handle->set_shm_key( handle, key );
    u_int32_t flags = DB_CREATE | DB_INIT_MPOOL | DB_INIT_TXN | DB_INIT_LOCK | DB_INIT_LOG | DB_SYSTEM_MEM;
    status = handle->open( handle, DIRECTORY, flags, 0644 );
    if( status != 0 )
    exit( 1 );
    // raise error
    chdir( DIRECTORY );
    for( int i=0; i < 0x1000; ++i )
    system( "db_stat -e | grep process/thread | wc" );
    return 0;
    }

    Hello,
    I am still looking into this, but upon closer examination here is the situation, basically as reported.
    The test calls set_thread_count which sizes the memory. In __env_attach(), (dbenv->thr_max is used to size the region. So you are correct, the higher the value used in set_thread_count, the larger the size of the region will be. Eventually that memory will be depleted, and __env_alloc_size() returns ENOMEM, after multiple calls to system(db_stat). __env_alloc_free() is not called. This is the part I am still investigating.
    The test uses system to fork a separate processes for db_stat and the failing stack is:
    lt-db_stat: assert failure: ../dist/../env/env_failchk.c/426: "ret == 0"
    lt-db_stat: __os_stack+0x2b
    lt-db_stat: __os_abort+0x1d
    lt-db_stat: __db_assert+0x47
    lt-db_stat: __env_set_state+0x53c
    lt-db_stat: __env_attach_regions+0x38b
    lt-db_stat: __env_open+0x154
    lt-db_stat: __env_open_pp+0x334
    lt-db_stat: lt-db_stat
    So db_stat is opening an environment, attaching to its region, and then __env_set_state() calls __env_alloc(). It is __env_alloc() which fails with ENOMEM after some point when there is no more space to be allocated from the shared region. And I'm still looking into that space would be returned to the shared region.
    Thanks,
    Sandra

  • Question about using threads and synchronizing objects

    Hi all,
    I am not that familiar and I have 2 questions.
    QUESTION 1
    I have a server which just sits and accepts incomming connections, then I spawn off a new thread which implements Runnable, and then call the run method. But the loop would not iterate, it would just sit inside the run method (which has rather long loop in it). I changed the class to extend Thread and called the start() method, rather than run(), and everything worked out fine.
    Anyone know why this is?
    QUESTION 2
    So I am building a client server chat application where a user can be in multiple chat rooms. I have this personObject which has output/input streams. In the personObject, I create writeObject/ readObject method, which simply does as it implies.
    I figured that I should make these methods synchronized, to ensure things run smoothly. however, when I made a call to readObject, which sat there and waited, and then made a call to writeObject, I would not enter the writeObject method. I took out the synchronized on the writeObject, and everything worked out fine.
    I was under the assumption that synchronizing a method only synchronized on that method, not on the whole class. How come then was I not able to enter the writeObject method?
    The more I think about it, I don't think I need to have these methods synchronized, but I thought it wouldn't hurt.
    Thanks for the help.

    Hi all,
    I am not that familiar and I have 2 questions.
    QUESTION 1
    I have a server which just sits and accepts incomming
    connections, then I spawn off a new thread which
    implements Runnable, and then call the run method.
    But the loop would not iterate, it would just sit
    inside the run method (which has rather long loop in
    it). I changed the class to extend Thread and called
    the start() method, rather than run(), and everything
    worked out fine.
    Anyone know why this is?
    You should still implement Runnable, rather than extending Thread. Like this: Runnable r = new MyRunnable();
    Thread t = new Thread(r);
    t.start(); run() is just another method. Nothing special about it as far as multithreading or anything else goes.
    start() is the method that actually spawns a new thread of execution.
    I was under the assumption that synchronizing a
    method only synchronized on that method, not on the
    whole class. How come then was I not able to enter
    the writeObject method?When you synchronize, you obtain a lock. As long as you hold that lock--until you exit the sync block or call wait()--no other thread can enter that block or any other block that obtains the same lock.
    Two synchronized methods on the same object rely on obtaining the same lock--there's one lock for each object.

  • Trying to download updated version of Adobe Flash player and "Blocked-Plug-In" keeps coming up and I

    Trying to download updated version of Adobe Flash Player on my MacBook Air, and "Blocked Plug-In" keeps coming up and I cannot download.

    After downloading, you need to install Flash Player. Double-click the downloaded file to install it.
    Please refer to the following thread for more details: http://forums.adobe.com/message/4651137
    Thanks,
    Sunil

  • Db_checkpoint: Unable to allocate thread control block

    Hello,
    Every second day BDB 4.8 runs out of cache memory (size = 512MB).
    We have 10 static worker processes (no segfaults) and one
    db_checkpoint-process (checkpoints once a minute and exists).
    We use only DB_WRITE_NOSYNC
    After two days the db_checkpoint-process reports:
    db_checkpoint: Unable to allocate thread control block
    As soon as this error apears, I can neither checkpoint nor
    db_recover.
    Is there any chance (or a patch) to track down the memory leak?
    Thanks

    Hi Sandra,
    It happened again one hour ago. I had hardly problems with BDB 4.6
    Down below you will find a current "db_stat -e"
    We have 10 workers with persistent db-connection and once a minute
    a cronjob starts a checkpoint, then the cron-process exists.
    Every second day (40 to 48 hours) the checkpoint-process can't connect
    to the database and "db_checkpoint -1" says:
    db_checkpoint: Unable to allocate thread control block
    A normal "db_recover" would corrupt the database, I had to recover
    catastrophic.
    For sure the cache is not to small (512 MB, all database files: 1,3 GB)
    Thanks
    Markus
    Mon Nov 30 17:32:44 2009 Local time
    0x120897 Magic number
    0 Panic value
    4.8.24 Environment version
    9 Btree version
    9 Hash version
    1 Lock version
    15 Log version
    4 Queue version
    2 Sequence version
    1 Txn version
    Mon Nov 30 16:31:06 2009 Creation time
    0x9d316701 Environment ID
    2 Primary region allocation and reference count mutex [0/360 0% !Own]
    11 References
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Thread tracking information
    75 Thread blocks allocated
    4096 Thread allocation threshold
    521 Thread hash buckets
    Thread status blocks:
    process/thread 5055/3086948768: out
    process/thread 5686/3086850464: out
    [...snip...]
    process/thread 6056/3086653856: out
    process/thread 6087/3086649760: out
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    0x40988 Log magic number
    15 Log version number
    16MB Log record cache size
    0 Log file mode
    10Mb Current log file size
    370618 Records entered into the log
    370MB 354KB 853B Log bytes written
    4MB 466KB 1001B Log bytes written since last checkpoint
    17691 Total log file I/O writes
    0 Total log file I/O writes due to overflow
    198 Total log file flushes
    665 Total log file I/O reads
    1786 Current log file number
    8303086 Current log file offset
    1786 On-disk log file number
    3630597 On-disk log file offset
    1 Maximum commits in a log flush
    1 Minimum commits in a log flush
    17MB Log region size
    9 The number of region locks that required waiting (0%)
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    5387 Last allocated locker ID
    0x7fffffff Current maximum unused locker ID
    9 Number of lock modes
    131072 Maximum number of locks possible
    131072 Maximum number of lockers possible
    131072 Maximum number of lock objects possible
    30 Number of lock object partitions
    622 Number of current locks
    797 Maximum number of locks at any one time
    13 Maximum number of locks in any one bucket
    0 Maximum number of locks stolen by for an empty partition
    0 Maximum number of locks stolen for any one partition
    632 Number of current lockers
    708 Maximum number of lockers at any one time
    69 Number of current lock objects
    160 Maximum number of lock objects at any one time
    2 Maximum number of lock objects in any one bucket
    0 Maximum number of objects stolen by for an empty partition
    0 Maximum number of objects stolen for any one partition
    13M Total number of locks requested (13372706)
    13M Total number of locks released (13372083)
    0 Total number of locks upgraded
    5313 Total number of locks downgraded
    9 Lock requests not available due to conflicts, for which we waited
    1 Lock requests not available due to conflicts, for which we did not wait
    0 Number of deadlocks
    60M Lock timeout value (60000000)
    0 Number of locks that have timed out
    60M Transaction timeout value (60000000)
    0 Number of transactions that have timed out
    66MB 904KB The size of the lock region
    2141 The number of partition locks that required waiting (0%)
    465 The maximum number of times any partition lock was waited for (0%)
    0 The number of object queue operations that required waiting (0%)
    3 The number of locker allocations that required waiting (0%)
    0 The number of region locks that required waiting (0%)
    2 Maximum hash bucket length
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    320MB 1KB 604B Total cache size
    1 Number of caches
    1 Maximum number of caches
    320MB 8KB Pool individual cache size
    0 Maximum memory-mapped file size
    0 Maximum open file descriptors
    0 Maximum sequential buffer writes
    0 Sleep after writing maximum sequential buffers
    0 Requested pages mapped into the process' address space
    17M Requested pages found in the cache (99%)
    71264 Requested pages not found in the cache
    67 Pages created in the cache
    71264 Pages read into the cache
    17944 Pages written from the cache to the backing file
    0 Clean pages forced from the cache
    0 Dirty pages forced from the cache
    0 Dirty pages written by trickle-sync thread
    71298 Current total page count
    71014 Current clean page count
    284 Current dirty page count
    32771 Number of hash buckets used for page location
    4096 Assumed page size used
    16M Total number of times hash chains searched for a page (16751183)
    8 The longest hash chain searched for a page
    39M Total number of hash chain entries checked for page (39437498)
    0 The number of hash bucket locks that required waiting (0%)
    0 The maximum number of times any hash bucket lock was waited for (0%)
    7 The number of region locks that required waiting (0%)
    0 The number of buffers frozen
    0 The number of buffers thawed
    0 The number of frozen buffers freed
    71627 The number of page allocations
    0 The number of hash buckets examined during allocations
    0 The maximum number of hash buckets examined for an allocation
    0 The number of pages examined during allocations
    0 The max number of pages examined for an allocation
    0 Threads waited on page I/O
    0 The number of times a sync is interrupted
    Pool File: article.db
    4096 Page size
    0 Requested pages mapped into the process' address space
    1868517 Requested pages found in the cache (99%)
    13311 Requested pages not found in the cache
    0 Pages created in the cache
    13311 Pages read into the cache
    3428 Pages written from the cache to the backing file
    [...snip...]
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    1786/3630541 File/offset for last checkpoint LSN
    Mon Nov 30 17:32:01 2009 Checkpoint timestamp
    0x8000675a Last transaction ID allocated
    4096 Maximum number of active transactions configured
    0 Active transactions
    3 Maximum active transactions
    26458 Number of transactions begun
    0 Number of transactions aborted
    26458 Number of transactions committed
    0 Snapshot transactions
    0 Maximum snapshot transactions
    0 Number of transactions restored
    1MB 192KB Transaction region size
    0 The number of region locks that required waiting (0%)
    Active transactions:
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    31MB 32KB Mutex region size
    3 The number of region locks that required waiting (0%)
    4 Mutex alignment
    150 Mutex test-and-set spins
    254163 Mutex total count
    149165 Mutex free count
    104998 Mutex in-use count
    104998 Mutex maximum in-use count
    Mutex counts
    149165 Unallocated
    1 env region
    33 lock region
    797 logical lock
    1 log filename
    1 log flush
    1 log region
    74 mpoolfile handle
    71298 mpool buffer
    17 mpool file bucket
    32771 mpool hash bucket
    1 mpool region
    1 mutex region
    1 transaction checkpoint
    1 txn region

  • Pass messages between main thread and FX application thread

    I'm launching an FX Application thread from a Main thread using Application.launch [outlined here: {thread:id=2530636}]
    I'm trying to have the Aplication thread return information to the Main thread, but Application.launch returns void. Is there an easy way to communicate between the Main thread and the Application thread?
    So far I have googled and found:
    - MOM (Message Orientated Middleware)
    - Sockets
    Any thoughts/ideas/examples are appreciated - especially examples ;) - right now I am looking at using Sockets to show/hide the application and for passing data.
    What is the preferred method? Are there others which I have not found (gasp) via Google?
    Dave.
    Edited by: cr0ck3t on 30-Apr-2013 21:04
    Edited by: cr0ck3t on 30-Apr-2013 21:05

    Is there an easy way to get a reference to these objects from both the Main thread and the FX Application thread - called via Application.launch() from the Main thread? Or do I have to use Sockets or MOM?Not much to do with concurrent programming is what I would call easy. It seems easy - but it's not.
    You can kind of do what you are describing using Java concurrency constructs without using sockets or some Message Oriented Middleware (MOM) package.
    With the Java concurrency stuff you are really implementing your own form or lightweight MOM.
    If you have quite a complex application with lots of messages going back and forth then some kind of MOM package such as camel or ActiveMQ (http://camel.apache.org) is useful.
    You can find a sample of various thread interactions with JavaFX here:
    https://gist.github.com/jewelsea/5500981 "Simulation of dragons eating dwarves using multiple threads"
    Linked code is just demo-ware to try out different concurrency facilities and not necessarily a recommended strategy.
    If your curious, you could take a look at it and try to work out what it is, what it does and how it does it.
    The main pattern followed is that from a blocking queue:
    http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/BlockingQueue.html
    Note that once you call launch from the main thread, no subsequent statements in the main method will be run until the JavaFX application shuts down. So you can't really launch from the main thread and communicate with a JavaFX app from the main thread. Instead you need to spawn another thread (or set of threads) for communication with the JavaFX app.
    But really, in most cases, the best solution with concurrency is not to deal with it at all (or at least as little as possible). Write everything in JavaFX, use the JavaFX animation framework for timing related stuff and use the JavaFX concurrency utilities for times when you really need multiple thread interaction.
    http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm
    To get further help, you might be better off describing exactly (i.e. really specific) what you are trying to do in a new question, perhaps with a sample solution in an sscce http://sscce.org

  • Java thread synchronized block

    i am still a noob comes to thread and synchronized.
    i heard people said that i shouldn't synchronized on "mySet" because "mySet" is getting modified in the synchronized block.
    is this true?
    or should i use a different object to synchronized the block. what's the advantage or disadvantage to use a different object to synchronized the block.
    thanks
    public class Test {
      final Set mySet = new HashSet(); 
      final Map myMap = new HashMap();
      public void add(Object o) {
        synchronize(mySet) {
          mySet.add(o);
          myMap.add(o);
      public void clear(){
        synchronize(mySet) {
          mySet.clear();
          myMap.clear();
    }

    yaliu07 wrote:
    i am still a noob comes to thread and synchronized.
    i heard people said that i shouldn't synchronized on "mySet" because "mySet" is getting modified in the synchronized block.
    is this true? No, that's not a reason not to sync on that object. In fact, in some cases it's a reason why you should sync on that object.
    Most of the time it doesn't matter which object you sync on, as long as all the relevant blocks are synced on the same object. There are a couple of considerations to keep in mind though that might affect the choice of object.
    In some cases, you want to sync on an object that's not exposed to the outside world, for instance a private Object member variable that exists only to be a lock. The reason for this is that if you sync on something that's exposed to the outside (such as "this", which is what gets synced on when you declare a non-static method as synchronized), then other code might also sync on that object, causing deadlock or thread starvation.
    In other cases, you DO want to expose the object, so that you can use it to ensure that other code and your synced code only enter the critical sections one thread at a time. For instance, in Collections.synchronizedXxx() methods, the return object's methods are all synced on "this", and any code that iterates over that collection must also sync on that object, so that the iteration code and the collection object's own code don't step on each other's toes.
    Much of the time, however, the choice of an object to sync on is just one that's convenient and logical for the humans writing and reading the code.

  • I need to multiplex communication between threads and sockets

    Hello,
    I need to implement a server which will have a maximum of 10 clients connected.
    I wanted to go for classic blocking I/O because I won't have much clients.
    My problem is that even if I accept() some sockets and then create a thread for each socket,
    then I still need to communicate some data to the other threads, problem is that I am blocked
    on the read() operation, how could I address this problem?
    I mean, lets say I am thread1 and blocked on read() on my socket, now thread7 wants to tell
    me something like a chat message coming from client he handles, how could thread7 manage
    to wake thread1 and send him a message somehow?
    I do not want to deal with NIO stuff if possible because I do not need to scale, I just have a
    problem because I am stuck on the read.
    I will run on Windows.
    Thanks for help.
    Edited by: Marzullo on Jul 15, 2010 1:05 PM

    Marzullo wrote:
    Hello,
    I need to implement a server which will have a maximum of 10 clients connected.
    I wanted to go for classic blocking I/O because I won't have much clients.
    My problem is that even if I accept() some sockets and then create a thread for each socket,
    then I still need to communicate some data to the other threads, problem is that I am blocked
    on the read() operation, how could I address this problem?For small apps like that, I've found it helpful to have two threads per socket: 1 reader and 1 writer. The reader can block on read() 99% of the time, while the writer is blocked on LinkedBlockingQueue.take() 99% of the time (or Object.wait() depending). The writer's queue is populated from the outside with whatever messages are destined to that socket.
    I mean, lets say I am thread1 and blocked on read() on my socket, now thread7 wants to tell
    me something like a chat message coming from client he handles, how could thread7 manage
    to wake thread1 and send him a message somehow?If thread1 is blocked on read() then it will receive any data that was sent to it, there's no need to wake it up because it gets what it's been waiting for (data on the input stream). Thread7 just needs to write data to the socket's output stream and the other end wakes up automatically because read() only blocks until data is available.

  • Differences between Synchronized methods and blocks

    Hi all,
    I would like to differences between Synchronized methods and blocks.
    - Muni

    Well, you'll get so many of right answers in next ten to thirty minutes.
    I like to yield :)
    Oooo... Ten minutes has passed.
    Synchronized block is a toilet room with a lock in a public lavatory.
    Only one person(running thread) can have the lock at a time.
    And, she/he have to receive the lock from a particular object obj like:
    synchronized(obj){.....} // one object has only one lock for this {} toilet and for any other toilets how many there are... *
    (*: In other words, while a thread is executing a synchronized(obj){} block, other threads can't enter other synchronized(obj){} blocks.)
    Synchronized method is a kind of synchronized block.
    This special block is defaulted to have lock from the object on which the method is defined.
    public class Foo{public synchronized Method(){}}
    use the lock of a Foo object.
    Message was edited by:
    hiwa
    Message was edited by:
    hiwa

Maybe you are looking for

  • Re: SAPlicense installation problem on NW04/WAS640/Linux

    Hi Friends, I have installed NW04/linux test drive on fedora linux. After that I try to install saplicense under user nw4adm. But I get following error: SAPLICENSE (Release 640) ERROR *** ERROR: Can not set DbSl trace function DETAILS: DbSlControl(DB

  • Multiple ICloud calendar issues?

    I have multiple iCloud calendars for my many  jobs and when I get a calendar invite It doesn't go to my default calendar and I cannot modify it. Any suggestions?

  • [SOLVED] Chromium won't redirect public wifi signup page, Firefox does

    Not sure if this belongs in the Apps forum or this one, let me know if this doesn't belong here. On my laptop, I connect to various Wifi networks at hotels, client networks, etc. Most of those Wifi networks have an "accept" or "enter your room #" typ

  • Some of my music is missing from iTunes Match and I can't add it manually.

    I seem to have remembered being able to manually add any new music I got to the cloud by simply right clicking on an album or a song in iTunes. Now it seems as if that doesn't work anymore. I just got some new music and it's not showing up in my iPho

  • New uses for an "extra" G3 Powerbook

    This may seem like a dumb question, but I have an "extra" G3 bronze keyboard Powerbook with OSX (Panther) running on it and I have no idea what to do with it. I upgraded last year to an iBook which I love, but I just can't seem to part with my first