Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在下面的多线程小程序中,是否只执行第一个线程?_Java_Multithreading_Applet - Fatal编程技术网

Java 在下面的多线程小程序中,是否只执行第一个线程?

Java 在下面的多线程小程序中,是否只执行第一个线程?,java,multithreading,applet,Java,Multithreading,Applet,该程序从发送方到接收方发送数据包,然后从接收方返回发送方。它对每个数据包使用线程,但只有第一个数据包在遍历路由 import java.applet.Applet; import java.awt.*; import java.util.ArrayList; import java.util.*; public class CongestionControl extends Applet { ArrayList<Packets> packetList = new ArrayL

该程序从发送方到接收方发送数据包,然后从接收方返回发送方。它对每个数据包使用线程,但只有第一个数据包在遍历路由

import java.applet.Applet;
import java.awt.*;
import java.util.ArrayList;
import java.util.*;

public class CongestionControl extends Applet {
   ArrayList<Packets> packetList = new ArrayList();
   static int width, height;
   int packetCount;
   static int sourceLocationX;
   private Label ls = new Label("Sender");
   private Label lr = new Label("Reciever");
   int sourceLocationY;
   int SWS;
   int packetWidth;
   int packetHeight;
   static int destLocationX;
   Random random = new Random();
   int RTT;

   public CongestionControl() {
      sourceLocationX = 100;
      sourceLocationY = 200;
      destLocationX = 400;
      RTT = 5;
      SWS = 4;
      packetWidth = 10;
      packetHeight = 10;
      packetCount = 10;
   }

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setLayout(null);
      for (int i = 0; i < packetCount; i++) {
         packetList.add(new Packets(sourceLocationX, sourceLocationY, Math
               .round(((2 * (-sourceLocationX + destLocationX)) / RTT) + 2)));
         // adding 2 purposely
      }
      add(ls);
      ls.setBounds(100, 240, 140, 30);
      add(lr);
      lr.setBounds(destLocationX + 20, 240, 140, 30);
   }

   private class MovingRunnable implements Runnable {
      private final Packets p;

      private MovingRunnable(Packets p) {
         this.p = p;
      }

      public void run() {
         for (;;) {
            p.move();
            repaint();
            try {
               Thread.sleep(200);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         }
      }
   }

   public void start() {
      for (Packets packet : packetList) {
         Thread t;
         t = new Thread(new MovingRunnable(packet));
         try {
            Thread.sleep(RTT / SWS); // packet generation interval
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         t.start();
         System.out.println("New thread generated");
      }
   }

   public void paint(Graphics g) {
      super.paint(g);
      for (Packets Packet : packetList) {
         g.drawRect(Packet.x, Packet.y, packetHeight, packetWidth);
         g.drawLine(sourceLocationX, sourceLocationY + packetHeight,
               destLocationX, sourceLocationY + packetHeight);
      }
   }

   public class Packets {
      int x;
      int y;
      int deltaX;
      int deltaY;

      public Packets(int startx, int starty, int deltax) {
         this.x = startx;
         this.y = starty;
         this.deltaX = deltax / 5;
      }

      public void move() {
         x += deltaX;
         if (x >= CongestionControl.destLocationX - packetWidth) {
            x = x - deltaX;
            deltaX = -deltaX;
            y += (packetHeight + 10);
         }
      }
   }
}
import java.applet.applet;
导入java.awt.*;
导入java.util.ArrayList;
导入java.util.*;
公共类拥塞控制扩展小程序{
ArrayList packetList=新的ArrayList();
静态int宽度、高度;
国际包裹计数;
静态int sourceLocationX;
专用标签ls=新标签(“发件人”);
专用标签lr=新标签(“接收器”);
int sourcelocation;
int SWS;
整数包宽度;
内包装高度;
静态int-deslocationx;
随机=新随机();
int RTT;
公共交通挤塞管制(){
sourceLocationX=100;
sourceLocationY=200;
去定位x=400;
RTT=5;
SWS=4;
包装宽度=10;
包装高度=10;
包数=10;
}
公共void init(){
宽度=getSize().width;
高度=getSize()。高度;
setLayout(空);
对于(int i=0;i=拥塞控制.destLocationX-packetWidth){
x=x-代尔泰;
deltaX=-deltaX;
y+=(包装高度+10);
}
}
}
}

为什么只有第一个线程在运行?

格式化后的代码很难阅读。请考虑编辑你的文章和修改你的缩进风格,使它是一致的和一致的。我通常避免使用制表符进行缩进(该网站的软件通常不能很好地使用制表符),并将每个代码块缩进4个空格,并且只缩进4个空格。编辑:这次我已经为您格式化了您的代码,但在将来,请您自己动手。这对你要求不高,而且会让你更容易得到帮助。