4.23 假努力第一天

集合多线程抽奖

public class MyThread extends Thread  {
    ArrayList <Integer> list;
    public MyThread(ArrayList<Integer> list){this.list=list;}
    @Override
     public void run(){
        while(true){
            synchronized (Thread.class) {
            if(list.size()==0){
                break;
            }else {
                Collections.shuffle(list);
                int prize=list.remove(0);
                System.out.println(getName()+"抽中了"+prize);
            }
            }
            try{
                Thread.sleep(10);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}


   public static void main(String[] args) {
      ArrayList list=new ArrayList<>();
      Collections.addAll(list,10,30,60);
      MyThread t1=new MyThread(list);
      MyThread t2=new MyThread(list);
      t1.setName("777");
      t2.setName("888");
      t1.start();
      t2.start();
   }

抽奖继续,求最大值 Callable

public class MyCallable implements Callable {
    ArrayList <Integer> list;
    public MyCallable(ArrayList<Integer> list){this.list=list;}
    @Override
     public Integer call() throws Exception {
        ArrayList<Integer> boxList = new ArrayList<>();
        while (true) {
            synchronized (MyCallable.class) {
                if (list.size() == 0) {
                    System.out.println(Thread.currentThread().getName() + "999");
                    break;
                } else {
                    Collections.shuffle(list);
                    int prize = list.remove(0);
                    boxList.add(prize);
                }
            }
            Thread.sleep(10);
        }
        if (boxList.size() == 0) {
            return null;
        } else {
            return Collections.max(boxList);
        }
    }
}


public static void main(String[] args) throws ExecutionException, InterruptedException {
      ArrayList <Integer> list=new ArrayList<>();
      Collections.addAll(list,10,30,80,900,80);
     MyCallable mc=new MyCallable(list);
      FutureTask <Integer> ft1=new FutureTask<>(mc);
      FutureTask<Integer> ft2=new FutureTask<>(mc);
      Thread t1=new Thread(ft1);
      Thread t2=new Thread(ft2);
      t1.start();
      t2.start();
      Integer max1=ft1.get();
      Integer max2= ft2.get();
      System.out.println(max1+"  "+max2);
   }

获取电脑的对象 主机和ip

 InetAddress address=InetAddress.getByName("LAPTOP-2ICLMU8T");
      System.out.println(address);
      String name= address.getHostName();
      System.out.println(name);
      String ip=address.getHostAddress();
      System.out.println(ip);

UDP 直接发送,而TCP 会连接两者是否连接成功

UDP:用户数据报协议,UDP面向无连接协议,速度快,最多64K,

TCP:传输控制协议,面向连接

下面代码发送和接受不知哪里出错了

public class Main {
   public static void main(String[] args) throws IOException {
     DatagramSocket ds=new DatagramSocket();
      Scanner sc=new Scanner(System.in);
      while(true){
         System.out.println("nixaingshuoshenme");
         String str=sc.nextLine();
         if("882".equals(str)){
            break;
         }
         byte[] bytes=str.getBytes();
         InetAddress address=InetAddress.getByName("192.1.168");
         int port=10086;
         DatagramPacket dp=new DatagramPacket(bytes, bytes.length,address,port);
         ds.send(dp);
      }
     ds.close();
   }
}



public class test extends Main {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10086);
        byte[] bytes=new byte[1024];
        DatagramPacket dp=new DatagramPacket(bytes,bytes.length);//??
        while(true){
            ds.receive(dp);
            byte[] data= dp.getData();
            int len=dp.getLength();
            String ip=dp.getAddress().getHostAddress();
            String name=dp.getAddress().getHostName();
            System.out.println(ip+"jijiji"+name+" "+len+" "+data);
        }
    }
}

socket和severSocket

 public static void main(String[] args) throws IOException {
        Socket socket=new Socket("192.1.168",1000);
        OutputStream os= socket.getOutputStream();
        os.write("aaa".getBytes());
        os.close();
        socket.close();
    }

ServerSocket ss=new ServerSocket(1000);
     Socket socket=ss.accept();
      InputStream is=socket.getInputStream();
      int b;
      while((b=is.read())!=-1){
         System.out.println((char)b);
      }

TCP三次握手,发送,确认,收回

TCP四次握手,服务器先发送我知道你的请求,服务器处理完后再发送信息我已经处理完了,你可以收回了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值