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, a
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
}
}