java 学习笔记6

//重载 (overload):方法的重载,方法名字相同时,根据参数的个数,类型不同,顺序不同,加载不同的方法。

//this:正在调用该方法的对象,正在初始化该方法的对象。
//super:直接父类对象的引用。通过super来访问父类中被子类覆盖的方法和属性。

//java中只有单继承,c++是多继承;java中的多继承,可以通过接口来实现

//重写(override):方法名称,参数列表和返回类型必须完全相同;且重写方法不能使用比父类方法更严格的访问权限(多态)

//一个类未使用extends关键字指明其基类,则默认基类为Object

//内部类:
//1.成员内部类,定义在外部类内部,看似像是外部类的一个成员一样。非静态内部类;静态内部类;局部内部类,定义在方法内部
Nose n = new Face().new Nose();

//非静态内部类调用外部类属性
Face.this.member;
//2.匿名内部类(没有定义用变量来存储,只使用一次new,也就是只需要使用一次的类)
function(new Class(){...});

//两个内容相同的对象,应该具有相等的hashcode值

//I/O 重点是形象思维:把Stream当做是一个管道,程序是一主体。
//输出流不管是writer还是outputStream ,在close()之前都应该flush(),因为在关闭之前可能还存在有buffer数据.
OutputStream os = new FileOutputStream();
os.write();
os.flush();
os.close();
//节点流:
//文件 File FileReader;FileInputStream
//         FileWriter;FileOutputStream
//内存数组 Memory Array CharArrayReader ByteArrayInputStream
//                     CharArrayWriter ByteArrayOutputStream
//内存字符串 Memory String StringReader
//                        StringWriter
//管道 Pipe PipedReader PipedInputStream
//         PipedWriter PipedOutputStream

//缓冲流:
//BufferedReader BufferedWriter BfferedInputStream BufferedOutputStream

//转换流:
//InputStreamReader OutputStreamWriter

//数据流:
//DataInputStream DataOutputStream

//打印流:不会抛异常;有自动flush功能
//PrintWriter PrintStream

//对象流:
//ObjectStream

//Serializable接口,标记性接口,表明可以序列化,没有任何的方法,是给编译器看的。
//transient 透明的,修饰成员变量,目的是在序列化的时候不予考虑。
//externalizable接口,自己控制自己的序列化过程。

//多线程
class Runner implements Runnable{
    public void run(){
        
    }
}
Runner r = new Runner();
Thread t = new Thread(r);
t.start();

this.wait();//当前线程锁定的对象停止,必须被synchronized修饰;wait(),互斥锁不再被其修饰对象所有;且必须被notify()唤醒.
this.notify();//唤醒
sleep(long mils);//static 线程睡眠多少毫秒数;互斥锁仍然
thread.join();//在当前线程执行点合并thread线程。
thread.yield();//让出当前线程所占用CPU
thread.setPriority(Thread.NORM_PRIORITY);//提高线程优先级,1-10,实质为CPU分配更多的时间片.

//互斥锁synchronized,锁住当前代码块或对象。
public synchronized void function(){};
synchronized(this){};

//死锁,deadlock,两个线程或者多个线程,同时需要对方正在使用的资源对象才能完成当前对象的互斥。

//生产者消费者问题,即线程同步问题。
public class ProducerConsumer{
    public static void main(String[] args){
        SyncStack ss = new SyncStack();
        Producer p = new Producer(ss);
        Consumer c = new Consumer(ss);
        Thread t1 = new Thread(p);
        Thread t2 = new Thread(c);
        t1.start();
        t2.start();
        //new Thread(p).start();
      //  new Thread(p).start();
       // new Thread(p).start();
    }
}

class WoTou{
    int id;
    WoTou(int id){
        this.id = id;
    }
    
    public String toString(){
        return "WoTou : "+id;
    }
}

class SyncStack {
    int index = 0;
    WoTou[] arrWT = new WoTou[6];
    
    public synchronized void push(WoTou wt){
        while(index == arrWT.length){
           try{
            this.wait();
           }catch(InterruptedException e){
               e.printStackTrace();
           }
        }
        this.notify();
        arrWT[index] = wt;
        index ++;
    }
    
    public synchronized WoTou pop(){
       while(index == 0){
           try{
            this.wait();
           }catch(InterruptedException e){
               e.printStackTrace();
           }
        }
        this.notify();
        index --;
        return arrWT[index];
    }
}

class Producer implements Runnable{
    SyncStack ss = null;
    Producer(SyncStack ss){
        this.ss = ss;    
    }
    
    public void run(){
        for(int i=0;i<20;i++){
            WoTou wt = new WoTou(i);
            ss.push(wt);
            System.out.println("生产了:"+wt);
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){
                 e.printStackTrace();
            }
            
        }
            
    }
}

class Consumer implements Runnable{
    SyncStack ss = null;
    Consumer(SyncStack ss){
        this.ss = ss;    
    }
    
    public void run(){
        for(int i=0;i<20;i++){
            WoTou wt = ss.pop();
            System.out.println("消费了:"+wt);
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){
                 e.printStackTrace();
            }
          
        }
            
    }
}
//socket编程



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值