启动将由多个线程更新的类时出现问题

发布于 2024-10-30 02:40:16 字数 2108 浏览 1 评论 0原文

您好,我正在尝试创建一个服务器/客户端程序,该程序最多需要 5 个客户端,每个客户端通过多个服务器端线程输入一个字符串,这些字符串将被添加到团队类中,即同一个团队,然后当团队已满时,客户端断开连接,服务器等待下一个团队的名称。

我的问题在于创建每个线程更新到的 Team 类的实例。 我不确定在哪里声明该类的实例?(它包含一个字符串数组[5]) 我在服务器端的类当前是“TeamServer”,“TeamServerThread”“team”“streamSocket”

下面是我的“TeamServerThread”当前获取用户字符串并将其添加到另一个字符串。

import java.io.*;

class TeamServerThread implements Runnable {
    static  String names ="";

   MyStreamSocket myDataSocket;

   TeamServerThread(MyStreamSocket myDataSocket) {
      this.myDataSocket = myDataSocket;
   }

   public void run( ) {
      String newName;
      try {
          newName = myDataSocket.receiveMessage();
/**/      System.out.println("Name Recieved = "+newName);


          updateNames(newName);
          // now send the names to the requestor
          // wait
          myDataSocket.sendMessage(names);
          myDataSocket.close();
      }// end try
      catch (Exception ex) {
           System.out.println("Exception caught in thread: " + ex);
        } // end catch
   } //end run

   private synchronized void updateNames (String newName) {
       names +=newName + "\n";
      // this.team.add(newName);
    } // end updateNames        
} // end Team

这是我的“团队”课程

public class Team 
    {
        public final int TEAM_SIZE = 5;

        public String names[] = new String[TEAM_SIZE];
        public int num_members = 0;
        // waits until five names have arrived
        // needs to be synchronized because it can be accessed
        // by a number of concurrent threads

        synchronized void  add(String name) 
        {
            names[num_members] = name;
            num_members++;
            if (num_members < TEAM_SIZE) 
            try 
            {
                wait(); 
            }
            catch(Exception e) {}
            else  
            try 
            {
                notifyAll(); 
            }
            catch(Exception e){}
        } // end add
        public int Getnum_members() 
        { 
            return num_members;
        }



} // end Team 

Hi i'm trying to create a sever/client program that takes up to 5 clients inputting a string each via multiple server side threads, these strings are to be added to team class ie the Same team, then when the team is full, clients disconnect and the server awaits names for the next Team.

My problem lies in creating an instance of the class Team that each thread updates to..
im not sure where to declare the instance of the class?(it contains a string array[5])
my classes on serverside are currently "TeamServer", "TeamServerThread" "team" "streamSocket"

Below is my "TeamServerThread" currently taking the user string and just adding it to another string.

import java.io.*;

class TeamServerThread implements Runnable {
    static  String names ="";

   MyStreamSocket myDataSocket;

   TeamServerThread(MyStreamSocket myDataSocket) {
      this.myDataSocket = myDataSocket;
   }

   public void run( ) {
      String newName;
      try {
          newName = myDataSocket.receiveMessage();
/**/      System.out.println("Name Recieved = "+newName);


          updateNames(newName);
          // now send the names to the requestor
          // wait
          myDataSocket.sendMessage(names);
          myDataSocket.close();
      }// end try
      catch (Exception ex) {
           System.out.println("Exception caught in thread: " + ex);
        } // end catch
   } //end run

   private synchronized void updateNames (String newName) {
       names +=newName + "\n";
      // this.team.add(newName);
    } // end updateNames        
} // end Team

Here is my "Team" class

public class Team 
    {
        public final int TEAM_SIZE = 5;

        public String names[] = new String[TEAM_SIZE];
        public int num_members = 0;
        // waits until five names have arrived
        // needs to be synchronized because it can be accessed
        // by a number of concurrent threads

        synchronized void  add(String name) 
        {
            names[num_members] = name;
            num_members++;
            if (num_members < TEAM_SIZE) 
            try 
            {
                wait(); 
            }
            catch(Exception e) {}
            else  
            try 
            {
                notifyAll(); 
            }
            catch(Exception e){}
        } // end add
        public int Getnum_members() 
        { 
            return num_members;
        }



} // end Team 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2024-11-06 02:40:16

所有类加载都是单线程的,您不能使用多个线程加载/更新类。不过我认为这不是你的意思。

你需要让每个套接字都可以看到的团队。您遇到的问题是您没有同步访问或替换团队,因此您可能会遇到竞争条件,即太多客户端尝试将自己添加到同一团队中。

我建议您有某种类型的 TeamCoordinator,它被传递到每个套接字连接,它可以确定何时应将客户端添加到 Team。

All class loading is single threaded and you cannot laod/update a class with multiple threads. However I assume this is not what you mean.

You need to have the Team where every socket can see it. The problem you have is that you haven't synchronized access or replacement of the Team so you could ahve a race condition where too many client try to add themselves to the same team.

I suggest you have some type of TeamCoordinator which is passed to each socket connection which can determine when Team a client should be added to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文