concurrent writes to a logfile from multiple threads (Java 7)?

Hello everyone

I have a multi-threaded Java 7 (jar file) program that uses JDBC to perform work (he uses a fixed pool of threads).

The program works very well and it records things that it moves the console command shell window (System.out.printf ()) starting from multiple concurrent threads.

In addition to the release of the console, I also need to add the capability of this program write in a single flat ASCII text file journal - starting from multiple threads.

The output volume is low, the file will be relatively low because it is not a data file, a log file.

Can you please suggest a relatively simple and good to achieve design/approach using Java 7 features (I have not yet Java 8)?

The code examples would also be appreciated.

Thank you very much

I have a multi-threaded Java 7 (jar file) program that uses JDBC to perform work (he uses a fixed pool of threads).

The program works very well and it records things that it moves the console command shell window (System.out.printf ()) starting from multiple concurrent threads.

In addition to the release of the console, I also need to add the capability of this program write in a single flat ASCII text file journal - starting from multiple threads.

The output volume is low, the file will be relatively low because it is not a data file, a log file.

Can you please suggest a relatively simple and good to achieve design/approach using Java 7 features (I have not yet Java 8)?

The code examples would also be appreciated.

Use a dedicated thread for logging and a queue to source it. See the "queue implementations of the trails in the Java tutorials for the use of queues and the code example that works.

https://docs.Oracle.com/javase/tutorial/collections/implementations/queue.html

Java has several classes of queue synchronized you can use if those in this tutorial are not suitable for your use case. Here's just one:

https://docs.Oracle.com/javase/7/docs/API/Java/util/concurrent/ConcurrentLinkedQueue.html

Unlimited thread-safe queue based on linked nodes. This queue orders elements FIFO (first-in-first-out). The head of the queue is this element which remained the longest the queue. The tail of the queue is this element that has been on the queue as quickly as possible. New elements are inserted at the tail of the queue, and get the recovery operations of the queue of the elements at the head of the queue. At ConcurrentLinkedQueue is a good choice when the number of threads will share access to a common collection. Like most other concurrent collection implementations, this class does not allow the use of null elements.

Using a queue your current code can add recording which is FAST because it is add an instance of the class to a collection rather interact with the file system. And your current code requires NO knowledge of the functioning of the system of newspaper, that it connects or when and where it connects.

For your use case use you a synchronized queue and edit each of your worker threads to add their log data to the master queue used by the newspaper for the logging thread.

You shouldn't have multiple threads trying to write directly in the same file.

The entire point of the modular and multi-processing programming is to BREAK dependencies between modules of code.

The logging system must be INDEPENDENT of the other code and your current code must NOT have ANY need or knowledge of the file system or OF the specificity of the way where, or when the data is actually stored.

Your code must run simple and minimal logging by adding its data to a queud. Once the log data was added to the queue and then, as far as your code is concerned, that data has been noted. It should be ONLY module log to determine the LOCATION to save the data, HOW to record data and even WHAT information to log.

You can add the larger newspapers in the future: newspapers, paper 1) different levels of 2) to a remote server or the database 3 send some events in the log.

I suggest you create a SIMPLE D.O. (data object) to maintain the log info (attributes like gravity, time/date, type of exception stack, error, etc.). Then create a class of SIMPLE log with several overloaded methods: a method that takes a single parameter, with two, etc. Javas is owner of exception classes have some simple examples of both simple and complex, nested exception handling.

These methods journal would take care to fill in the NEWSPAPER to DO correctly and then placing the instance of the D.O. to the journal queue. In this way, you can use a parameter of a simple newspaper just pass an error message and the log class method will add a default severity level (which can vary from thread if you want), time (which can range from wire) and other necessary data.

The goal is to keep the journal interface use you in your existing code as SIMPLE as possible while keeping as independent as possible of the actual logging mechanism.

The result is a mechanism of simple logging that is versatile and scalable and can then be used for your PROJECTS that require a registration (file, database, e-mail).

Do not make the mistake to hardcode the mechinism journal in your existing code.

DO not continue independent, modular architecture that you already use.

Tags: Java

Similar Questions

  • Fill a table from multiple threads

    Hello

    I tried to find the mechanism that I could use to fill an array of strings of multiple threads (a little and loops with different times, within a single process) in an asynchronous way.

    I went to create--> property/Invoke Nodes-> (...) but I could not find any properties/methods that I could use. Did I miss something?

    For example how programmatically insert item in array to the x position?

    I will use so semaphores or FGV would be enough?

    K


  • You bird 31 crashes when you reply to an e-mail from multiple threads

    I've updated to version 31 today. Since then, when replying to emails that have several threads (2 or 3 responses attached) the Send button hang up. If I wait long enough, or violin, it can go to the project. Why? What can I do to get this kind of threaded replies to send as an e-mail from original naked base?

    Davidk03

    do not plug into something new, I asked if you had it that it sounded like you did. You asked what it was that has basically answered my question, but I have provided a link to answer your question of what is.

    I have no idea of what having 2 or 3 responses attached, I thought that the add-on could explain it. So does that mean?

    Disable e-mail analysis:-almost all of the programs anti-virus mail as well as falling out.

    No one else has reported the problem, so unless your ready to work through the process and exactly what it IS your destiny to never upgrade to a newer version. Never. The question is no longer not studied before by someone, unless you have be it resolved.

  • Berkeley DB Java Edition - multiple threads accessing the database

    Hello

    I would like to access (read & write) a Berkeley DB of multiple threads (process/FMV anyway).

    Can I use a single com.sleepycat.je.Database object for all threads, or should I create an instance of each?

    Or is there another way to do it?

    Thank you

    The object database can be used by multiple threads, you can use a single instance or multiple ones, as suits you best. Of http://docs.oracle.com/cd/E17277_02/html/java/com/sleepycat/je/Database.html.

    The database handles are free of threads and can be used simultaneously by multiple threads. '

    If you read the javadoc, you can find any other comments on the use of the classes from multiple threads.

    Kind regards

    Linda

  • Problem with multiple threads

    I have a program that interacts with an ESX 3.5 server using the VIX API, written in c#. I'm from multiple threads in c# program with each thread running an automation process which creates a connection to a virtual machine on the ESX Server, some automated process, and then disconnects. I'm having problems with this. When I call Disconnect() from one of the threads, it seems to cause problems with my other son if they have not yet disconnected. I connect to an another VM on the server in each thread. The problem is if I don't call disconnect, I get errors, but if I call disconnect in a thread it causes errors. Y at - it something similar? Any suggestions on how to make this run smoothly? Thank you!

    This sounds a lot like you are getting hurt by the host sharing of handle.  If HostConnect() is called with the same parameters, the handle of the host gets reused.  HostDisconnect() will clean this handle, even if his commune.  It's actually documented behaivior.  We have also decided that there may problems like the one you have reached, so it will be removed in a future release.

    So your application will share the handful of host among all discussions, and as soon as we call disconnection, the host handles in other countries also becomes invalid.

    Make a unique HostConnect() and share this handle inside threads is a way to avoid this.  HostConnect() is generally quite expensive, so we recommend in all cases.

  • SQL Toolkit crashing with multiple threads

    Hello everyone and happy new year!

    I was hoping someone might be able to shed some light on this problem. I update an older application to use multiple threads. In fact, the thread that is causing a problem now is created by using an asynchronous timer.

    I use CVI 2010, and I think the SQL Toolbox is version 2.2.

    If I execute a SQL statement from the main thread, there is no problem.

    stat = DBInit (DB_INIT_MULTITHREADED);
    
    hdbc = DBConnect( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=sample.mdb;Mode=ReadWrite|Share Deny None" );
    hstmt = DBActivateSQL( hdbc, "SELECT * FROM SAMPLES" );
    
    DBDeactivateSQL(hstmt);
    DBDisconnect(hdbc);
    

    If I add code to perform the same duties in a reminder of the timer, it causes a stack overflow error.

    .. Start the main thread

    stat = DBInit (DB_INIT_MULTITHREADED);
    
    NewAsyncTimer (5.0, -1, 1, &gfn_quicktest, 0);
    

    .. end of main thread

    .. and then the reminder of the timer

    int CVICALLBACK gfn_quicktest (int reserved, int timerId, int event,  void *callbackData, int eventData1, int eventData2)
    {
      {
        int hdbc = DBConnect( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=params\\sample.mdb;Mode=ReadWrite|Share Deny None" );
        int hstmt = DBActivateSQL( hdbc, "SELECT * FROM SAMPLES" );
    
       DBDeactivateSQL(hstmt);
        DBDisconnect(hdbc);
      }
    
      return 0;
    }
    

    The program crashes with a stack overflow error when the DBActivateSQL statement is called.

    I understand that the Access ODBC driver can not support multithreaded, but I only connect to this base starting from the same thread with these 2 statements only so it should be good?

    Any idea would be appreciated,
    Thank you
    Ed.

    I just tried this using the sample provided with CVI access database. It uses a DSN instead of mdb. It has worked out well. I see that no reason multithreading would be a problem here, if you open and close the connection in the same segment of code. I see that you use params in the asynchronous callback connection string. Where did this come from? Maybe try to use the database and see if it works.

  • Update multiple columns from multiple tables in a single UPDATE request

    Hello

    I'm trying to figure if I'm heading in the right direction.

    I want to update multiple columns from multiple tables in a single UPDATE request. Also, I would like to update multiple columns in a table from the tables.

    Scenario 1

    UPDATE Table2, Table 3
    SET T2.Column1 = T1.Column1 
    ,T2.Column2 = T1.Column2
    ,T3.Column2 = T1.Column2
    FROM Table1 T1, Table2 T2, Table3 T3
    WHERE T1.id = T2.id
    and T1.id = T3.id
    
    

    Scenario 2

    UPDATE Table3
    SET T3.Column1 = T1.Column1 
    T3.Column2 = T1.Column2
    ,T3.Column3 = T2.Column3
    ,T3.Column4 = T2.Column4
    FROM Table1 T1, Table2 T2, Table3 T3
    WHERE T3.id = T1.id
    and T3.id = T2.id
    
    

    Hello

    For scenario 1, you must write separate instructions UPDATE table2 and table3.

    To guard against someone else change one of these tables while you act so you can copy all relevant data in a global temporary table and update this global temporary table table3.

    ENGAGE only when two tables have been changed.

    You can write a procedure or an INSTEAD OF trigger to do all this.

    For scenario 2, you can reference many tables that you need when new table3.  It might be more efficient and simpler to use the MERGER rather than UPDATED.  For example:

    MERGE INTO table3 dst

    WITH THE HELP OF)

    SELECT t1.id

    t1.column1

    t1.column2

    t2.column3

    t2.column4

    FROM table1 t1

    JOIN table2 t2 ON t1.id = t2.id

    )             src

    WE (dst.id = src_id

    WHEN MATCHED THEN UPDATE

    SET dst.column1 = src.column1

    dst.column2 = src.column2,

    dst.column3 = src.column3,

    dst.column4 = src.column4,

    ;

  • The write button has disappeared from the taskbar. How can I get that back so I can send emails?

    The write button has disappeared from the taskbar. How can I get that back so I can send emails?

    Thank you very much. It worked! You are a genius! A lot of people in computer science I know tried to solve the problem and he could not understand. Once again, thanks a lot!

  • How to move photos from multiple folders into one

    Hello!

    I exported photos of iPhotos to a folder on the Mac.

    He saved every moment in different folders.

    I want to combine them all, so that all the photos are in a single folder.

    How can I combine these multiple files from multiple folders into one?

    Thank you very much for your help!

    (PS next time I will not include fracture m´by moments, but for now, how do I fix?)

    Drag and drop one folder to the other.

  • AppleScriptObjC and multiple threads

    Hello again people.

    I read some old threads here to have multiple threads in a Cocoa-AppleScript application, but I have not found any that I think that would work for my particular use case.

    Here's my situation: I have a method that expects that the user say a phrase and then runs another method when he hears the phrase. Here is the code that I currently use:

    on listenForQuery()
    set query to ""
            try
                tell application "/System/Library/PrivateFrameworks/SpeechObjects.framework/Versions/A/SpeechRecognitionServer.app"
                    set query to listen for {"Hello"} giving up after 5
                end tell
            end try
            if query as text is equal to "Hello" then my newQuery_(null)
            performSelector_withObject_afterDelay_("listenForQuery", missing value, 2.0) -- do it again every two seconds
        end listenForQuery
    

    AppleScriptObjC is not multi-threaded, but it seems that you forgot that it can use many of the classes and methods of cocoa.  Instead to use AppleScript to control another application, take a look at aid NSSpeechRecognizer in yours - you can give it a list of commands to listen and to define a delegate method that is called when something is recognized.

  • L50-A-19N satellite can not read audio data from multiple sources

    I can't read the audio data from multiple sources. It is very annoying when I have 2 youtube videos, playing, if I start playing something on the media player, there is no sound on media player, it's the same when I have 2 open media players and 1 youtube video playing, youtube video has no sound...
    It disappears when I plug my headphones...

    I already have all the latest drivers, the DTS driver was last updated was in 2014, his day of February of this year...






    25/02/14

    DTS Inc. Windows 8.1 - 64 Bit 1.01.2700

    I don't know if this has the feel, but I had his most recent DTS driver that I found, it is not my laptop model, but they all seem to be the same - v1.1.88.0
    I uninstalled the DTS software and still had the same problem, then it is irrelevant on its driver somehow...






    02/10/15

    Integrated Device Technology Inc. Windows 8.1 - 64 Bit 6.10.6491.0

    Audio driver IDT has more recent release date, but the version of the driver is the same as the 2013 one...
    Why the older drivers of toshiba releaseing as 'NEW '?

    2nd is my Advanced settings speakers, nothing has changed when I disabled "allow applications to take exclusive control of this device.

    Sorry but I don't understand your problem.
    I tested it on my machine and if I start the music on three different sources (YouTube, player, web radio) I can hear all together, but it makes no sense to listen to music from different sources.

    Or how do understand you?

  • Is it possible to synchronize music and photos from iTunes from multiple computers?

    We have two MacBook Pro - one for me and one for my children.

    Currently, I manage all the music and pictures via my Mac (synchronization of iOS devices), so there is a place for all the music and photos.

    However, children now want to be able to manage their own music and photos.

    Is it possible to synchronize music or photos from multiple computers? for example:

    -Sync Mac and pictures of the other music

    -Synchronize Mac music

    Thank you

    The key thing here is not the machines it's libraries. Devices can synchronize an iTunes library at a time. You have to arrange if both computers have the same library (problems with maintaining them synchronized or using at the same time), or seek a centralized service such as game or music from Apple (with her own set of questions).  The key is that the media distributors wish to exercise tight control over how music and movies can be shared (they don't care about your photos).  You can do things with the homepage of sharing with family.

    Family shares - http://support.apple.com/kb/HT201060 - "family sharing makes it easy for up to 6 people in your family share of each and other iTunes, iBooks, and App Store purchases without sharing accounts.» Families with the same credit card purchases and approve kids' expenses directly from the device of a parent. And share photos, a family calendar and more to help keep everyone connected. »

    «Sharing content with sharing - http://support.apple.com/HT201085 - bought family"with the participation of the family, family members go immediately to each and any other music, movies, TV shows, books and applications and can download to their devices at any time they souhaitez.*" ' * not all content and content types are available in all countries.» All members of the family must use the same iTunes Store country or region. Music, movies, TV shows and books can be downloaded on up to 10 devices per account, of which five may be computers. Apps can be downloaded from the other facilities, family member owns or controls.

    If you do not see your family sharing content - http://support.apple.com/kb/TS5449 also: http://apple.stackexchange.com/a/146077 - exit iTunes Store and App Store apps and start over.

    Who bought content can I share using sharing - Apple-Support - http://support.apple.com/HT203046 family

    Let family sharing - http://support.apple.com/HT201081

    Home sharing Support page - http://www.apple.com/support/homesharing/

    12 iTunes for Mac: use the home sharing to import items from another iTunes library - https://support.apple.com/kb/PH19614

    iTunes: set up sharing at home on your computer - http://support.apple.com/kb/HT4620

    Understand sharing home - http://support.apple.com/kb/HT3819

  • Writes a text file from a network computer to another computer on the network.

    It is possible to write a text file from a computer on the network to another computer on the network through labview without using FTP?

    Hi bebo_lm,

    It depends on how they are connected. If you have a 'normal' access to the pc and you can write on a hard drive of the other computer, you can use the writing functions of files or the copy of the file. If they are not connected like this, then you need another service. Why you do not use FTP?

    Mike

  • method accewwing from another thread causes some 'bad object type' error with c# VS2008

    I'm snapping a picture to a main Viewer, draw a rectangle around an alignment and extract the part of the image to a different viewer for further processing. I tried everthing and all was good until I started to build a Wizard for the operator through the process of the market and do things automatically. If I have access to this method of the mainform thread, everything works fine. If I access it from the thread of substantive assistants work it fails and gives the error "you have provided the wrong type of object to the function. Spend a CWIMAQRectangle instead. »

    public int ExtractToGP1()

    {

    CWIMAQLearnPatternOptions LearnOptions = new CWIMAQLearnPatternOptions();
    CWIMAQRectangle RoiRectangle = new CWIMAQRectangle();

    int Count, result;

    Extract the region corresponding to the model selected in the main Viewer

    Result = axCWMachineVision1.GetSelectedRectangleFromViewer (axCWIMAQViewer1.GetCWIMAQViewer (), RoiRectangle, true, 1); //<--error>

    Result = axCWIMAQVision1.Extract2 (axCWIMAQViewer1.Image, axCWIMAQViewer5.Image, RoiRectangle, 0, 0);

    I tried to use:

    axCWMachineVision1.Invoke (delegate (MethodInvoker)

    {Result = axCWMachineVision1.GetSelectedRectangleFromViewer (axCWIMAQViewer1.GetCWIMAQViewer (), RoiRectangle, true, 1) ;})} ;

    axCWMachineVision1.Invoke (delegate (MethodInvoker)

    {Result = axCWIMAQVision1.Extract2 (axCWIMAQViewer1.Image, axCWIMAQViewer5.Image, RoiRectangle, 0, 0) ;})} ;

    but this only works if the method is called by the main thread.

    Resolved to myself. Not really a problem of OR.

    I moved

    CWIMAQLearnPatternOptions LearnOptions = new CWIMAQLearnPatternOptions();

    CWIMAQRectangle RoiRectangle = new CWIMAQRectangle();

    for my vision instead of having it in the method class, which is in the mainform class

    Then use Vision.RoiRectangle vs RoiRectangle and Vision.LearnOptions vs LearnOptions.

  • MenuItems run() method that is called from the thread eventdispatching?

    Hello

    is it? I know that MenuItem itself extends thread, but I wonder why this method is called run() and finally and above all it would'nt be worse if this method would be called from a Thread.

    THX

    In fact the menu items are run on the thread of the event. You can see this by creating a menu item long-term. The menu will not close and will not refresh the screen.

Maybe you are looking for

  • Recently, I can't send a picture of small volume without her dividing enamel in parts, and the jpeg file that I sent is received in the form of numbers.

    It's something that started recently... tell a month ago. I sent 2 less than 600 KB images which were divided into 3 groups, I guess by firefox. When the email arrived, they were not photos but the numbers. Previously, I had sent pictures of several

  • I just dropped my phone?

    I just dropped my iPhone 6 + didn't break the screen, but the touch screen does not work and theres a ink large spill what should I do? How much will cost of difficulty?

  • application builder error.llb

    Hi all I use the application builder to compile an executable from my VI. Compilation works without a hitch. However, when I try to launch the .exe subvis are missing three of error.llb: not found Dialog.vi, Dialog.vi to view details and the value st

  • controlling supreme (1)

    Hi I have a game controlling supreme called TI installed very well on xp and windows 7, but when I try to play online multyplayer with Buddy on windows 7, it does not work it keeps on saying it tries to download a patch, he did the same after that I

  • Bug in Vista sp2

    It seems that XPS Document Viewer is not backward compatible after installing the Vista SP2. When a document is signed digitally with Vista SP2, the signature cannot be seen with Windows XP, the viewer of XPS documents. First of all, let me say: If t