在下面的多线程小程序中,只有第一个线程被执行?

时间:2015-04-19 12:33:03

标签: 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 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);
         }
      }
   }
}

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

0 个答案:

没有答案