Basic java sockets

Hi mọi người , đây là dự án socket chat thu gọn của em

Em có 2 vấn đề

  1. Khi 1 user load lịch sử mà có tin nhắn mới từ user khác gửi tới sẽ gây chen tin nhắn mới vào chuỗi tin lịch sử
    Em đã xử lý bằng cách chuyển nội dung từ server chuyển về thành JSON và từ JSON đọc type bên trong , input sẽ phân tích type chứa nội dụng là "current hay “history” và chuyển vào queue khác nhau . Từ đây luồng input hoạt động liên tục mà vẫn đảm bảo phân lọc đúng current và history để đẩy lên UI một cách song song cả 2 tin nhắn của 2 thời điểm

  2. Khi 2 user cùng gửi tin nhắn tới nhau tại 1 thời điểm thì việc khởi tạo ID sẽ bị trùng lặp khi vào file lưu
    Em đã xử lý bằng cách gắn sync cho phương thức khởi tạo ID , vì mỗi client sẽ có 1 thread riêng nên thread nào vào trước sẽ phải thread còn lại làm xong

ko biết 2 cách giải quyết 2 vấn đề có được tối ưu ko ? và cần cải thiện gì cho 2 cách trên ko ạ , em cảm ơn mọi người nhiều

The more I read your package, the more I believe that your Java knowledge is patchy.

  1. Threads are expensive and resource-intensive. The more threads are involved, the greater the risk of overwriting each other (poor concurrency or parallelism). Instead of an extra thread (IOClientHandler), why don’t you use the ExecutorService to start the client that has arrived? An ArrayList is not only superfluous, but it makes the separation between “me” and “the others” more difficult.
  2. The extra (IOClient)Thread causes problems with synchronization because you no longer know which thread belongs to whom and thus the risk of overwriting/intermixture becomes omnipresent (see 1).
  3. File IOs are not completely protected from overwriting/intermixture by a synchronized() block. A separate protection procedure must be developed here.
  4. Why can’t the ClientManager be started directly as client, but by an extra app? And that worsens the overview so much that you don’t even know which client is doing what.
  5. BlockingQueue is good for modification (add, remove), but not secure for reading or addAll(). You have to implement a ReentrantLock, for example, so that reading is more secure.

Thank you for your help

1 + 3 + 5 : oke , i will consider what you advise and apply it again to my project

4 : In fact, each client of a user will be a different device and each different device is a separate main, so I separate the classes containing ClientManager as a practical example, right?

2 : from 4 , every time a class containing ClientManager runs

is a starting device of the starting user

from here there will be 2 separate threads for that client (IO), 2 threads belonging to that client

You don’t seem to understand what I’m talking about. The principle of client/server is that each client gets a server worker spawned by the server and both live and work with each other while the worker takes care of communication with other clients for its own partner on the server’s side. Instead of sticking with the easy way, you try to invent a new way where you don’t even know what’s going on inside your app and I’m not going to rewrite your app.

Sorry. I think you should consider whether to continue as an IT developer or choose an easier profession that requires less abstract imagination than IT.

I’ll stop helping you here and wish you all the best in making the right choice for your future,

UPDATE:

In fact, each client of a user will be a different device and each different device is a separate main, so I separate the classes containing ClientManager as a practical example, right?

Different device? Gosh, since when does the server care about the hardware the client is running on and your app uses the same ClientManager just with a string parameter userID. So what’s the difference? JAVA is hardware independent and that’s the main reason why JAVA is so widely used in the developer community.

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?