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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:06:27+00:00 2026-06-14T07:06:27+00:00

For my current java exercise, I have to get mail from 2 different gmail

  • 0

For my current java exercise, I have to get mail from 2 different gmail accounts. I have done this by creating new instances of my gmail class. The gmail class extends thread, and within it there is a synchronized method readMail() which gets the mail and prints it. This readMail method is called by the run method in a while(true) loop, and then it sleeps for 30 seconds, the idea being that it gets mail every 30 seconds. However, the synchronized method does not seem to work. The threads interrupt each other and the method does not print all the items of a message before the other thread interrupts and starts printing.

Any advice would be greatly appreciated.

Please see the method below that is causing me trouble:

public synchronized void readMail() throws MessagingException, IOException {
    Folder inbox = store.getFolder("Inbox");
    inbox.open(Folder.READ_ONLY);
    messages = inbox.getMessages();
    // System.out.println("No of Messages : " + inbox.getMessageCount());
    // System.out.println("No of Unread Messages : "
    // + inbox.getUnreadMessageCount());

    for (int i = 0; i < inbox.getUnreadMessageCount(); i++) {

        System.out
                .println("*****************************************************************************");
        System.out.println("NEW MESSAGE " + (i + 1) + ":");
        msg = messages[i];
        // System.out.println(msg.getMessageNumber());
        // Object String;
        // System.out.println(folder.getUID(msg)

        String subject = msg.getSubject();

        System.out.println("Subject: " + subject);
        System.out.println("From: " + msg.getFrom()[0]);
        System.out.println("To: " + msg.getAllRecipients()[0]);
        System.out.println("Date: " + msg.getReceivedDate());
        System.out.println("Size: " + msg.getSize());
        // System.out.println(msg.getFlags());
        // System.out.println("Body: \n"+ msg.getContent());
        // System.out.println(msg.getContentType());
    }
}

Then the run method:

    public void run() {
    while (true) {

        try {
            readMail();
        } catch (MessagingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}
  • 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-14T07:06:29+00:00Added an answer on June 14, 2026 at 7:06 am

    you should synchronize your method on an object both threads have access to, because right now you are using the object instance you are in to synchronize which of cause will never have an effect because both Threads only stay inside their on scope as far as I understand your question. You could pass an simple Object to both threads at their creation and reformat your method like this

    passing object to synchronize:

    public static void main(String[] args){
         Object obj = new Object();
         gmail g1 = new gmail(obj);
         gmail g2 = new gemail(obj);
         // more code
    }
    

    save reference in gmail class:

    public class gmail extends Thread{
        private Object sharedObject;
    
        public gmail( Object synchronizer){
              sharedObject = synchronzier;
        }
    

    synchronize on it:

    public void readMail(){
       synchronized( sharedObject ){
           // your method code goes here
       }
    }
    

    For this Example synchronizing on the class object of gmail is also possible and is even more easy

    public void readMail(){
       synchronized( this.getClass() ){
           // your method code goes here
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my current project I have to send a signature from PHP to Java
We have a current Java EE project and a new requirement to introduce a
How can I change the current working directory from within a Java program? Everything
How do I get the current stack trace in Java, like how in .NET
I need to get the AST for the current selection in the java editor
This is taken from Exercise 19 of The Pragmatic Programmer. A quick reality check.
Being new to scala and a current java developer, scala was designed to encourage
I have installed Eclipse on Debian Lenny after having installed java (apt-get install sun-java6-jre
I'm planing / designing my database for my current project (Java). For this project
I am doing a little programming exercise in Java to get me back into

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.