Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:54:49+00:00 2026-06-15T10:54:49+00:00

I am trying to write Observe pattern in Java in multithread program to develop

  • 0

I am trying to write Observe pattern in Java in multithread program to develop own logging program. My program is used in any Java class and can create the Logger class, and write a message to the logger. Logger class will call the logger manager class , which is a Singleton class and it also have the private class which is thread, on execution a scheduler runs, So I kept that execution in the private constructor.

private void LoggerManager() { 
    System.out.println("IN CONSTRUCTOR"); 
    executorThread.submit(new CreateLoggerFileForSpecifiedTime());
}

static LoggerManager getInstance() {
    if (LOGMANAGER == null) {
        synchronized (LoggerManager.class) {
            if(LOGMANAGER == null){
                System.out.println("IN MANAGER");
                LOGMANAGER = new LoggerManager();
            }
        }
    }
    return LOGMANAGER;
}

Next there is a method in the LoogerManager “writeMessageToLog”.

void writeMessageToLog(String componantName, String message, 
                     Calendar messageCreationTime) { 
    LoggerDetails logDetails = new LoggerDetails(componantName, message,
                     messageCreationTime); 
    LogInitiater logIntiater = new LogInitiater(logDetails, noticeOfLoggerChange, 
                     noticeOfMessageAdded); 
    executorThread.submit(logIntiater); 
} 

LogIntitiater is a thread which is added the LogWriter to two Subject’s, One subject is a scheduler I have pasted above i.e a Timer runs for every specified time and notfiies LogWriter the need to create a new log file while the other purpose is when a message is added in the queue. Code is not working as expected:
* scheduler code is not running, I tried with execute as well. That private constructor is not being called
* thread initiater is not always called
* observe pattern: I have used J2SE interfaces i.e Observable and Observe and they are not working as expected

Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:54:50+00:00Added an answer on June 15, 2026 at 10:54 am

    Producer – Consumer way for Logger (INCOMPLETE AND NOT FULLY TESTED) – will leave that to you

    This is just for the demonstration purpose and shows one way of doing ASYNCHRONOUS logging

    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.BlockingQueue;
    
    class Logger extends Thread{
            private static Logger logger = null;
            private BlockingQueue<String> logRequests;
    
            private Logger(){
                logRequests = new ArrayBlockingQueue<String>(10000);
            }
    
            public static Logger getLogger(){
                if(logger == null){
                    synchronized (Logger.class) {
                        logger = new Logger();
                        logger.start();
                    }
                }
                return logger;
            }
    
    
            @Override
            public void run() {
                System.out.println("Starting to log");
                while(true){
                    try {
                        System.out.println(logRequests.take());
                    } catch (InterruptedException e) {
                        //interrupted so stop logging
                        System.out.println("Logger exiting");
                        break;
                    }
                }
            }
    
            public void info(String logMessage){
                //not taken care of the case when queue becomes full
                //will leave it to you
                logRequests.add(logMessage);
            }
        }
    

    Main class

    public static void main(String[] args) {
            Logger.getLogger().info("This is info message");
    
            System.out.println("I am free");
    
    
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
                //stop the log
            Logger.getLogger().interrupt();
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to write a chat, like on facebook, I wondered if two clients can
I'm trying to write a plugin for a Java Application. The plugin should be
I'm trying write a C program that sends an UDP packet to a given
I am trying write a program in python to calculate heart rate training zones
I'm trying write a program that takes a list of names, sorts those names,
I'm trying to write an object relationship that follows an observer pattern where the
I'm trying to write a program that will allow me to give it a
I'm trying write a test for a method that returns a java.sql.Connection to connect
I am trying write a script where when a user clicks on a link,
I m trying write code that after reset set up rrpmax as 3000. It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.