Renaissance
Renaissance

Reputation: 554

Serialize output in arraylist

I have three class:

Chatroom_Server.java this is main class which will excecutes first.

public class Chatroom_Server {

     public static ChatroomHandler chatroom_handler;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        try{
            final int serverPort = 9000;
            ServerSocket ser_sock = new ServerSocket(serverPort);
            chatroom_handler = new  ChatroomHandler();
            chatroom_handler.fetchChatroomList();
            for(int i=0;i<chatroom_handler.chatroomlist.size();i++){
                Chatroom_base obj = (Chatroom_base)chatroom_handler.chatroomlist.get(i);
                System.err.println(obj.chatroom_name);
            }

        }catch(Exception e){
            System.out.println(e);
        }
    }

}

Second class is ChatroomHandler.java

public class ChatroomHandler{
    public static ArrayList<Chatroom_base> chatroomlist;
    DbConnection dbcon;
    ChatroomHandler(){
        //load all chatrooms from the database and add those into arrayList
        dbcon = new DbConnection();
        chatroomlist = new ArrayList<Chatroom_base>();


    }
    public void fetchChatroomList(){
        ArrayList<String> chtrmlist = dbcon.getChatroomList();

        for(int i = 0;i<chtrmlist.size();i++){
            System.out.println("Chatroom name123:"+chtrmlist.get(i));
            Chatroom_base obj = new Chatroom_base();
            synchronized(obj) {
                obj.setChatroomname(chtrmlist.get(i));
                chatroomlist.add(obj);
            }

        }
    }
}

Third class is Chatroom_base.java:

public class Chatroom_base{
    public static ArrayList<Socket> chatSocketArray;
    public static ArrayList<String> userInfo;
    public static String chatroom_name;
    private static final long serialVersionUID = 123456;
    Chatroom_base(){
        userInfo = new ArrayList<String>();
        chatSocketArray = new ArrayList<Socket>();

    }
    public synchronized void setChatroomname(String tname){
        System.out.println("Chatroom name:"+tname);
        chatroom_name = tname;
    }
}

Fourth class is DbConnection.java in which I did the database connectivity part.
I am getting the output in the below given sequence.

Chatroom name123:common
common3
common3
Chatroom name:common
common3
Chatroom name123:common2
Chatroom name:common2
Chatroom name123:common3
Chatroom name:common3

I guess this is the serialization issue. Processes are getting the resource before the time. I want to get output like:

   Chatroom name123:common
    Chatroom name:common
    Chatroom name123:common2
    Chatroom name:common2
    Chatroom name123:common3
    Chatroom name:common3
    common
    common2
    common3

I have very few ideas about java. Can I get some idea or direction to solve this problem?

Upvotes: 0

Views: 28

Answers (1)

Laszlo Valko
Laszlo Valko

Reputation: 2743

You should remove static from these fields:

public class Chatroom_base{
    public static ArrayList<Socket> chatSocketArray;
    public static ArrayList<String> userInfo;
    public static String chatroom_name;

These are object fields, not class fields, so they should not be static.

Upvotes: 1

Related Questions