原 自学JVAVA---(61)--(内功心法【59】)深度观察线程状态

深度观察线程状态
【小城贝尔】
线程生命有轨迹,
API中寻机密。
新生new字状态类,
get方法来买醉。
RUN后NABLE两种态,
运行就绪均涵盖。
睡眠加入时间待,
等待流向是阻塞。


public class ThreadState {
    public static void main(String[] args) {
        boolean flag = true;
        Thread t = new Thread(()->{
            for (int i = 0; i <=20; i ++){
                System.out.println("learning state .....");
                if(i % 4 == 0){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        System.out.println("thread's state ....is  ... "+t.getState());
        t.start();
        System.out.println("thread's state ....is  ... "+t.getState());
        while (flag){
            if(t.getState().toString().equals("TERMINATED")){
                break;
            }
            System.out.println("around get State ..."+t.getState());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
/*    
    thread's state ....is  ... NEW  新生状态
    thread's state ....is  ... RUNNABLE 运行状态 包含就绪状态 和运行状态
    around get State ...RUNNABLE
    learning state .....
    around get State ...TIMED_WAITING 阻塞状态 包含sleep方法 join方法
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    around get State ...RUNNABLE RUNNABLE 运行状态 包含就绪状态 和运行状态
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    around get State ...TIMED_WAITING 阻塞状态 包含sleep方法 join方法
    learning state .....
    around get State ...RUNNABLE RUNNABLE 运行状态 包含就绪状态 和运行状态
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    learning state .....
    around get State ...RUNNABLE RUNNABLE 运行状态 包含就绪状态 和运行状态
    around get State ...RUNNABLE RUNNABLE 运行状态 包含就绪状态 和运行状态
    
    BLOCKED :阻塞状态包含 wait ,流操作read write方法等
 */           
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,可以使用InputStream和OutputStream类来读写二进制文件。InputStream用于读取二进制数据,而OutputStream用于写入二进制数据。下面是一个简单的示例代码,展示了如何使用InputStream和OutputStream读写二进制文件: ```java import java.io.*; public class BinaryFileReadWrite { public static void main(String[] args) { String filePath = "example.bin"; try { // 创建二进制文件并写入数据 OutputStream outputStream = new FileOutputStream(filePath); byte[] data = {1, 2, 3, 4, 5}; outputStream.write(data); outputStream.close(); // 读取二进制文件 InputStream inputStream = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int bytesRead = inputStream.read(buffer); while (bytesRead != -1) { // 处理读取到的数据 for (int i = 0; i < bytesRead; i++) { System.out.print(buffer[i] + " "); } bytesRead = inputStream.read(buffer); } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例代码中,我们首先使用OutputStream来创建一个名为"example.bin"的二进制文件,并写入一些数据。然后,我们使用InputStream来读取该文件中的数据,并将其打印到控制台上。注意,我们使用了一个字节数组作为缓冲区,每次读取最多1024个字节,并且在读取完整个文件之后,需要关闭InputStream对象。 当然,这只是一个简单的示例,实际的二进制文件读写可能涉及到更复杂的数据结构和算法,需要根据具体情况进行设计和实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值