首页 > 编程语言 >Java第十八课_线程和网络

Java第十八课_线程和网络

时间:2023-12-26 21:44:59浏览次数:39  
标签:Java Thread 第十八 System 线程 println new public out

1.线程

  • 线程的休眠

        public static void main(String[] args) {
            // 线程的休眠
            // 第一种方式(不推荐):
            try {
                Thread.sleep(3000);// 毫秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 第二种方式 :
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
        }
    
  • 插队

    
        public static void main(String[] args) {
            // 联合/插队 : join(); 其他线程先暂停, 让join线程先运行一段时间
    
            Thread thread = new Thread(() -> {
                for (int i = 0; i < 50; i++) {
                    System.out.println(Thread.currentThread().getName() + "  ::  " + i);
                }
            }
            );
    
            thread.start();
    
            try {
                for (int i = 100; i < 130; i++) {
                    if (i == 108) {
                        System.out.println("插队开始....");
                        thread.join(0, 5);
                        System.out.println("时间到,插队结束~~~~~~~~~~~~~~~~~~~~~....");
                    }
                    System.out.println(Thread.currentThread().getName() + "  ::  " + i);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
  • 礼让

        public static void main(String[] args) {
    
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            // 线程的礼让 : yield(); , yield线程先暂停, 让其他线程先运行一段时间
            Thread thread = new Thread(() -> {
                for (int i = 0; i < 40; i++) {
                    System.out.println(Thread.currentThread().getName() + " ::: " + i + " ~~~ " + Thread.currentThread().getPriority());
                }
            });
            thread.setPriority(Thread.MIN_PRIORITY);
            thread.start();
    
            new Thread(() -> {
                for (int i = 100; i < 140; i++) {
                    System.out.println(Thread.currentThread().getName() + " ::: " + i + " ~~~ " + Thread.currentThread().getPriority());
                }
            }).start();
    
            for (int i = 200; i < 240; i++) {
                System.out.println(Thread.currentThread().getName() + " ::: " + i);
                Thread.yield(); // 看起来像是没有暂停时, 是因为其他线程的运行未出结果
                System.out.println(Thread.currentThread().getName() + " ::: " + i);
            }
        }
    
  • 线程安全01

    public class Ticket {
    
        private Integer id;
    
        public Ticket(Integer id) {
            this.id = id;
        }
    
        public Integer getId() {
            return id;
        }
    
        @Override
        public String toString() {
            return "Ticket{" +
                    "id=" + id +
                    '}';
        }
    }
    
    public class Station {
    
        private static List<Ticket> tickets = new ArrayList<>();
    
        static {
            for (int i = 0; i < 30; i++) {
                tickets.add(new Ticket(i + 1));
            }
        }
    
        public List<Ticket> getTickets() {
            return tickets;
        }
    }
    
    public class Practice01 {
    
        public static void main(String[] args) {
            /*
                线程安全问题:
                    多人同时买票, 多线程下如何保证票不重卖
    
                同步代码块 : 各线程使用到锁内资源时, 只能一个个单独访问(相当于多线程执行锁内代码时, 变回了单线程)
                 synchronized (锁) {
                    需要被同步的代码;
                 }
    
                 锁对象 : 所有线程看到的是同一个对象才会生效
                    1.字符串/String : 不推荐使用
                    2.当前类的字节码文件
                    3.this
             */
            Station station = new Station();
    
            SaleStationA saleStation = new SaleStationA(station);
    
            Thread thread1 = new Thread(saleStation,"飞猪");
            Thread thread2 = new Thread(saleStation,"携程~~~~~~");
            Thread thread3 = new Thread(saleStation,"12306");
            Thread thread4 = new Thread(saleStation,"去哪^_^");
    
            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();
        }
    }
    
    public class SaleStationA implements Runnable{
    
        private Station station;
    
        public SaleStationA(Station station) {
            this.station = station;
        }
    
        @Override
        public void run() {
            List<Ticket> tickets = station.getTickets();
            System.out.println(Thread.currentThread().getName() + " :: " + this);
            while (tickets.size() > 0) {
                // synchronized (锁)
                synchronized (this) {
                    if (tickets.size() > 0) {
                        Ticket ticket = tickets.remove(0);
                        System.out.println(Thread.currentThread().getName() + " A卖出了一张票 , 票号为 : " + ticket.getId());
                    }
                }
                // 模拟消耗时间
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
  • 线程安全02

    public class Practice02 {
        public static void main(String[] args) {
            /*
                锁的第二种实现方式:
                同步函数 :
                synchronized 返回值类型  函数名(){}
    
                锁对象 :
                    普通函数的锁对象是 this
                    静态函数的锁对象是当前类的字节码文件
             */
            Station station = new Station();
    
            SaleStationB saleStation1 = new SaleStationB(station);
            SaleStationB saleStation2 = new SaleStationB(station);
            SaleStationB saleStation3 = new SaleStationB(station);
            SaleStationB saleStation4 = new SaleStationB(station);
    
            Thread thread1 = new Thread(saleStation1,"飞猪");
            Thread thread2 = new Thread(saleStation2,"携程~~~~~~");
            Thread thread3 = new Thread(saleStation3,"12306");
            Thread thread4 = new Thread(saleStation4,"去哪^_^");
    
            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();
        }
    }
    
    public class SaleStationB implements Runnable{
    
        private Station station;
    
        public SaleStationB(Station station) {
            this.station = station;
        }
    
    
        @Override
        public void run() {
            List<Ticket> tickets = station.getTickets();
            System.out.println(Thread.currentThread().getName() + " :: " + this);
            while (tickets.size() > 0) {
    
                saleTicket(tickets);
                // 模拟消耗时间
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
        // 同步函数 :synchronized
        public static synchronized void saleTicket(List<Ticket> tickets){
            if (tickets.size() > 0) {
                Ticket ticket = tickets.remove(0);
                System.out.println(Thread.currentThread().getName() + " B卖出了一张票 , 票号为 : " + ticket.getId());
            }
        }
    }
    
  • 线程安全03

    public class Practice03 {
    
        public static void main(String[] args) {
            /*
                锁的第三种实现方式:
                    Lock:
                        用法类似第一种, 加上Lock声明, 之后的资源就只能单独访问了
                        有效访问到lock.unlock();为止
             */
    
            Station station = new Station();
    
            Lock lock = new ReentrantLock();
    
            SaleStationC saleStation1 = new SaleStationC(station,lock);
            SaleStationC saleStation2 = new SaleStationC(station,lock);
            SaleStationC saleStation3 = new SaleStationC(station,lock);
            SaleStationC saleStation4 = new SaleStationC(station,lock);
    
            Thread thread1 = new Thread(saleStation1,"飞猪");
            Thread thread2 = new Thread(saleStation2,"携程~~~~~~");
            Thread thread3 = new Thread(saleStation3,"12306");
            Thread thread4 = new Thread(saleStation4,"去哪^_^");
    
            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();
        }
    }
    
    public class SaleStationC implements Runnable{
    
        private Lock lock;
        private Station station;
    
        public SaleStationC(Station station ,Lock lock) {
            this.lock = lock;
            this.station = station;
        }
    
        @Override
        public void run() {
            List<Ticket> tickets = station.getTickets();
            System.out.println(Thread.currentThread().getName() + " :: " + this);
            while (tickets.size() > 0) {
    
                try {
                    // Lock
                    lock.lock();
                    if (tickets.size() > 0) {
                        Ticket ticket = tickets.remove(0);
                        System.out.println(Thread.currentThread().getName() + " C卖出了一张票 , 票号为 : " + ticket.getId());
                    }
                } finally {
                    lock.unlock();// 需要unlock声明锁到哪里
                }
                // 模拟消耗时间
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
  • 死锁

    public class Practice04 {
    
        public static void main(String[] args) {
            // 死锁 :
            // A 线程已经 占用 了 x 资源 , 需要 y资源才能继续执行 , 此时 , B线程 占有 y资源 ,需要 x资源才能继续执行
            class MyThread implements Runnable{
                @Override
                public void run() {
                    String name = Thread.currentThread().getName();
                    if ("小明".equals(name)) {
                        synchronized ("筷子A") {
                            System.out.println(name + " 持有筷子 A , 需要筷子B 才能吃东西");
                            synchronized ("筷子B") {
                                System.out.println("小明开始吃饭....");
                            }
                        }
                    }
                    if ("老王".equals(name)) {
                        synchronized ("筷子B") {
                            System.out.println(name + " 持有筷子 B , 需要筷子A 才能吃东西");
                            synchronized ("筷子A") {
                                System.out.println("老王      开始吃饭....");
                            }
                        }
                    }
                }
            }
    
            Thread thread1 = new Thread(new MyThread(), "小明");
            Thread thread2 = new Thread(new MyThread(), "老王");
            thread1.start();
            thread2.start();
        }
    }
    

2.网络

  • InetAddress

        public static void main(String[] args) throws UnknownHostException {
            // InetAddress : 此类表示互联网协议 (IP) 地址。
            // static InetAddress getLocalHost()
            //           返回本地主机信息。
            // UnknownHostException
            InetAddress inetAddress = InetAddress.getLocalHost();
    
            System.out.println("inetAddress = " + inetAddress);// 辉/192.168.1.2
            System.out.println("inetAddress.getHostAddress() = " + inetAddress.getHostAddress());// 192.168.1.2
            System.out.println("inetAddress.getHostName() = " + inetAddress.getHostName());// 电脑名
            System.out.println("inetAddress.getAddress() = " + inetAddress.getAddress());// [B@4554617c
            System.out.println("Arrays.toString(inetAddress.getAddress()) = " + Arrays.toString(inetAddress.getAddress()));
            //  [-64, -88, 1, 2]  : 192 - 128 - 128
    
            InetAddress byAddress = InetAddress.getByAddress(new byte[]{-64, -88, 1, 2});
            System.out.println("byAddress = " + byAddress);
    
            InetAddress byName = InetAddress.getByName("LAPTOP-D5AN7N74");
            System.out.println("byName = " + byName);
        }
    
  • Server和Client

    public class Server {
    
        public static void main(String[] args) throws IOException {
            // 1.创建服务器对象
            ServerSocket serverSocket = new ServerSocket(8888);
            // 2.开启侦听功能,等待客户端连接
            System.out.println("服务器已启动,等待客户端链接......");
            Socket socket = serverSocket.accept();
            // 3.获取输入流,读取客户端发送过来的信息
            InputStream inputStream = socket.getInputStream();
            // 包装为字符缓冲流
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String info = bufferedReader.readLine();
            System.out.println("我是服务端 , 客户端跟我说 : " + info);
    
            // 4.获取输出流响应客户端
            OutputStream outputStream = socket.getOutputStream();
            // 包装为字符缓冲流
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            String serverInfo = "lisi , 你好!! ";
            bufferedWriter.write(serverInfo);
            bufferedWriter.flush();
    
            // 5.资源释放
            bufferedWriter.close();
            outputStream.close();
            bufferedReader.close();
            inputStream.close();
            socket.close();
            serverSocket.close();
        }
    }
    
    public class Client {
    
        public static void main(String[] args) throws IOException {
            // 当前主机的表示形式 : 1.localhost  2.   127.0.0.1   3.IP地址 :  192.168.1.2
            // 1.客户端对象
            Socket socket = new Socket("localhost", 8888);// 主机和端口号都是服务器
            // 2.获取输出流发送信息
            OutputStream outputStream = socket.getOutputStream();
            // 3.包装为字符缓冲流, 向服务器发送信息
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            String info = "username=lisi&password=123";
            bufferedWriter.write(info);
            bufferedWriter.flush();
            socket.shutdownOutput();
    
            // 4.获取输入流,读取服务器信息
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String clientInfo = bufferedReader.readLine();
            System.out.println("我是客户端 , 服务器对我说  : " + clientInfo);
    
            // 5.释放资源
            bufferedReader.close();
            bufferedWriter.close();
            inputStream.close();
            outputStream.close();
            socket.close();
        }
    }
    
  • Server和Client02

    public class Server {
    
        public static void main(String[] args) throws IOException {
            // 1.创建服务器对象
            ServerSocket serverSocket = new ServerSocket(8888);
            // 2.开启侦听功能,等待客户端连接
            System.out.println("服务器已启动,等待客户端链接......");
            Socket socket;
            while (true) {
                socket = serverSocket.accept();
                new Thread(new ServerThread(socket)).start();
            }
        }
    }
    
    public class ServerThread implements Runnable{
    
        private Socket socket;
    
        public ServerThread(Socket socket) {
            this.socket = socket;
        }
    
        @Override
        public void run() {
            // 3.获取输入流,读取客户端发送过来的信息
            InputStream inputStream = null;
            // 包装为字符缓冲流
            BufferedReader bufferedReader = null;
            // 4.获取输出流响应客户端
            OutputStream outputStream = null;
            // 包装为字符缓冲流
            BufferedWriter bufferedWriter = null;
            try {
                inputStream = socket.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    
                String info = bufferedReader.readLine();
                System.out.println("我是服务端 , 客户端跟我说 : " + info);
    
                // 提取用户名
                String[] split = info.split("&");
                String[] split1 = split[0].split("=");
                String username = split1[1];
    
                // 获取用户 IP 地址
                InetAddress inetAddress = socket.getInetAddress();
    
                outputStream = socket.getOutputStream();
    
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
                String serverInfo = username + " , 你好!! 您的IP地址为 : " + inetAddress.getHostAddress();
                bufferedWriter.write(serverInfo);
                bufferedWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedReader.close();
                    inputStream.close();
                    bufferedWriter.close();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    }
    
    public class Client {
    
        public static void main(String[] args) throws IOException {
            // 当前主机的表示形式 : 1.localhost  2.   127.0.0.1   3.IP地址 :  192.168.1.2
            // 1.客户端对象
            Socket socket = new Socket("192.168.31.116", 8888);// 主机和端口号都是服务器
            // 2.获取输出流发送信息
            OutputStream outputStream = socket.getOutputStream();
            // 包装为字符缓冲流
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            String info = "username=kitty&password=123";
            bufferedWriter.write(info);
            bufferedWriter.flush();
            socket.shutdownOutput();
    
            // 3.获取输入流,读取服务器信息
            InputStream inputStream = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    
            String clientInfo = bufferedReader.readLine();
            System.out.println("我是客户端 , 服务器对我说  : " + clientInfo);
    
            // 4.释放资源
            bufferedReader.close();
            bufferedWriter.close();
            inputStream.close();
            outputStream.close();
            socket.close();
        }
    }
    
  • 爬虫

    public class Practice {
    
        public static void main(String[] args) throws IOException {
            // MalformedURLException
            // URL
            URL url = new URL("https://baike.baidu.com/item/%E6%A0%B9%E5%9F%9F%E5%90%8D%E6%9C%8D%E5%8A%A1%E5%99%A8/5907519?fromtitle=%E6%A0%B9%E6%9C%8D%E5%8A%A1%E5%99%A8&fromid=136331");
    
            InputStream inputStream = url.openStream();
    
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("baidu.html"));
    
            int len;
            byte[] bytes = new byte[1024 << 2];
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, len);
            }
            bufferedOutputStream.flush();
    
            bufferedOutputStream.close();
            bufferedInputStream.close();
            inputStream.close();
        }
    }
    

标签:Java,Thread,第十八,System,线程,println,new,public,out
From: https://www.cnblogs.com/shinya/p/17929428.html

相关文章

  • 无涯教程-Java9 - Stream API增强
    Java9改进的StreamAPI添加了一些便利的方法,使流处理更容易,并使用收集器编写复杂的查询。Java9为Stream新增了几个方法:dropWhile、takeWhile、ofNullable,为iterate方法新增了一个重载方法。takeWhile语法defaultStream<T>takeWhile(Predicate<?superT>predic......
  • Java 日期转换运算工具类
    ​  参考文档:Java日期转换运算工具类-CJavaPy1、Date常用方法java.util.Date 是Java中表示日期和时间的一个基础类。但随着时间的推移,许多其方法被标记为过时,并被推荐在Java8引入的 java.time 包中的新类替代。但是,Date仍然在很多旧的代码库中广泛使用,并在与某......
  • 线程通讯 网络编程
    //线程的休眠try{Thread.sleep(3000);//毫秒}catch(InterruptedExceptione){e.printStackTrace();}//第二种方式:try{TimeUnit.MINUTES.sleep(3);}catch(InterruptedExceptione){e.printStackTrace();} 联合/插队:join()Threadthread=new......
  • stream的优化:java封装的拆箱与装箱的弊端
    authors.stream().map(author->author.getAge).map(age->age+10)//Stream<Integer>.filter(age>18)//Stream<Integer>.foreach(System.out::println);上述是一个简单的stream流的使用,当我们拆开第二个map,或者filter的时候会发现,传入和传出的参数都是Intege......
  • java基础语法API之GUI图形化界面1
    一:概述虽然现在在开发中,前后端交互,用户所看到的界面都是前端实现的,但是java自身的图形化界面,对于java学习初学者还是需要了解的。对于开发是有利的。二:具体说明<1>GUI介绍java中为GUI相关的API在java.awt包和java.swing包中。java.awtawt是这三个单词首字母的缩写,翻译过来是抽象窗......
  • java中的方法引用
    方法引用是对lambda的进一步简化,如果方法体只有一个方法调用的话,可以直接采用对象名(类名)::方法名的格式(想起来了C++的一些语法)一个例子:上面是lambda的一种写法,下面是方法引用的改进int[]arr={-1,2,3,4,5,6};Arrays.stream(arr).forEach(a->System.out.println(a));......
  • JAVA 设计模式(一) - 单例模式
    JAVA设计模式(一)-单例模式本篇文章主要讲下android创建型设计模式中的单例模式.单例模式保证一个类只有一个实例,并提供一个访问该实例的全局节点.它的实现有多种实现方式:1:饿汉式饿汉式:类加载时就创建实例.不支持延迟加载实例publicclassSingleton{priva......
  • JAVA设计模式(三)-原型
    JAVA设计模式(三)-原型本篇文章主要讲下java创建型设计模式中的原型模式.何谓原型模式:简单来说就是将一个对象作为原型,通过对其进行复制而克隆出多个和原型类似的新实例。使用原型模式,就可以简化实例化的过程,不必依赖于构造函数或者new关键字.由于java提供了clone方法......
  • JAVA设计模式(二)-简单工厂
    JAVA设计模式(二)-简单工厂本篇文章主要讲下java创建型设计模式中的简单工厂模式.简单工厂模式又叫静态工厂方法模式(StaticFactoryMethodPattern),是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。1:定义接口publicinterfacePeople{v......
  • 无涯教程-Java9 - 私有接口方法
    在Java8之前,接口可以具有以下类型的变量/方法。Constantvariables(常量)Abstractmethods  (抽象方法)因此,无涯教程不能在接口中实现方法实现,或更确切地说,不能在Java8之前拥有默认实现。请参见示例。publicclassTester{publicstaticvoidmain(String[]args)......