java基础7

IO

1InputStream

1int read()

从输入流中读取数据的下一个字节。返回 0 255 范围内的 int 字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1

2int read(byte[] b)

从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。否则以整数形式返回实际读取的字节数。

3int read(byte[] b, int off,int len)

将输入流中最多 len 个数据字节读入 byte 数组。尝试读取 len 个字节,但读取的字节也可能小于该值。以整数形式返回实际读取的字节数。如果因为流位于文件末尾而没有可用的字节,则返回值 -1.

4public void close() throws IOException关闭此输入流并释放与该流关联的所有系统资源

 

 

  1. public class MyThreadWithExtends extends Thread {  

  2.   

  3.     private int tickets = 10;  

  4.   

  5.     @Override  

  6.     public void run() {  

  7.   

  8.         for (int i = 0; i <= 100; i++) {  

  9.             if(tickets>0){  

  10.                 System.out.println(Thread.currentThread().getName()+"--卖出票:" + tickets--);  

  11.             }  

  12.         }  

  13.     }  

  14.       

  15.       

  16.     public static void main(String[] args) {  

  17.         MyThreadWithExtends thread1 = new MyThreadWithExtends();  

  18.         MyThreadWithExtends thread2 = new MyThreadWithExtends();  

  19.         MyThreadWithExtends thread3 = new MyThreadWithExtends();  

  20.   

  21.         thread1.start();  

  22.         thread2.start();  

  23.         thread3.start();  

  24.           

  25.         //每个线程都独立,不共享资源,每个线程都卖出了10张票,总共卖出了30张。如果真卖票,就有问题了。  

  26.     }  

  27.   

  28. }  

 

 

  1. package multithreading;  

  2.   

  3. public class MyThreadWithImplements implements Runnable {  

  4.   

  5.     private int tickets = 10;  

  6.   

  7.     @Override  

  8.     public void run() {  

  9.   

  10.         for (int i = 0; i <= 100; i++) {  

  11.             if(tickets>0){  

  12.                 System.out.println(Thread.currentThread().getName()+"--卖出票:" + tickets--);  

  13.             }  

  14.         }  

  15.     }  

  16.       

  17.       

  18.     public static void main(String[] args) {  

  19.         MyThreadWithImplements myRunnable = new MyThreadWithImplements();  

  20. 这其中的Runnable仅仅是个业务,只不过放在Thread中,交给线程处理 

  21.         Thread thread1 = new Thread(myRunnable, "窗口一");  

  22.         Thread thread2 = new Thread(myRunnable, "窗口二");  

  23.         Thread thread3 = new Thread(myRunnable, "窗口三");  

  24.   

  25.         thread1.start();  

  26.         thread2.start();  

  27.         thread3.start();  

  28.     }  

  29.   

  30. }  

 

其他方法

1、public final boolean isAlive()判断线程是否还活着

2、public final String getName()返回线程的名称

3、public final void setName(String name)设置该线程名称

4、public static Thread currentThread()返回当前线程。在Thread子类中就是this,通常用于主线程和Runnable实现类

线程的优先级

线程的优先级控制

  •  

    MAX_PRIORITY(10);   

  •  

    MIN _PRIORITY (1); 

  •  

    NORM_PRIORITY (5);

涉及的方法:

  •  

    getPriority() :返回线程优先值

  •  

    setPriority(int newPriority) :改变线程的优先级

线程创建时继承父线程的优先级

说明:优先级只是代表概率,不代表绝对的先后顺序

解决办法线程问题:synchronized+wait+notify

  •  

    线程安全问题:同步

  •  

    透支消费问题:线程通信

  •  

    仓库容量有限问题:线程通信

wait() 与 notify() 和 notifyAll(),Java.lang.Object提供的这三个方法。

  •  

    public final void wait():该线程发布对此监视器的所有权(即释放锁)并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。

  •  

    public final void notify():唤醒在此对象监视器(锁)上等待的单个线程。如果所有线程都在此对象上等待,则会选择唤醒其中一个线程。选择是任意性的,并在对实现做出决定时发生。

  •  

    public final void notifyAll():唤醒在此对象监视器(锁)上等待(wait)的所有线程。

 

class HouseWare {

private final int MAX = 10;

private int num;

 

public synchronized void put() {

if(num>=MAX){

try {

this.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

num++;

System.out.println("工人生产了一台电视机,现在库存为:" + num);

this.notify();

}

 

public synchronized void take() {

if(num<=0){

try {

this.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

num--;

System.out.println("消费者买走了一台电视机,现在库存为:" + num);

this.notify();

}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值