Object笔记

Object为java最基础的类,所有的类都继承自Object,一般情况下可以忽略不写。

Object一共有九大函数

一、clone()

1、clone和copy的区别:

copy或者用一个对象给另一个对象赋值。两者会指向同一个变量。只要修改其中一个对象,则另一个对象也会被修改。

clone会创建一个新的变量,只是这个新的变量使用的是同样的值。

2、clone必须要implements Cloneable接口

否则,调用clone方法时,会发生CloneNotSupportedException

3、在调用super.clone需要抛出异常 CloneNotSupportedException

4、Shallow Clone与Deep Clone

即浅复制与深复制。对于引用对象,浅复制会指向同一对象

在对象中有引用对象时,需要对引用进行特殊处理

public Object clone() throws CloneNotSupportedException  {  
         Employee cloned = (Employee) super.clone();  
      cloned.hireDay = (Date) hireDay.clone()  
      return cloned;  
        } 

5、Object中的clone使用的是protected声明的,表示只能clone属于同一个类的对象

以Employee。

    类为例,通过声明为protected,就可以保证只有Employee类里面才能“克隆”Employee对象

 

二、toString()

Object 类的 toString 方法返回一个字符串,该字符串由类名(对象是该类的一个实例)、at 标记符“@”和此对象哈希码的无符号十六进制表示组成。

该方法用得比较多,一般子类都有覆盖

三、getClass()

 返回次Object的运行时类类型。

不可重写,要调用的话,一般和getName()联合使用,如getClass().getName();

四、finalize()

在垃圾回收器对对象进行内存回收时,会调用此方法

五、equals()

Object中的equals方法是直接判断this和obj本身的值是否相等

六、hashCode()

返回该对象的哈希码值

七、wait()    notify()  notifyAll

对象调用wait后,持有该对象锁的线程会进入等待,直到其他线程调用notify

public static void main(String[] args) {
		Wait wait = new Wait();
		Queue<Integer> queues = new LinkedList<Integer>();
		int maxSize = 3;
		Producter pro = new Producter(queues,maxSize,"Producter1");
		Customer customer1 = new Customer(maxSize, queues, "customer1");
		Customer customer2 = new Customer(maxSize, queues, "customer2");
		Customer customer3 = new Customer(maxSize, queues, "customer3");

		pro.start();
		customer1.start();
		customer2.start();
		customer3.start();
	}
	
	
}
class Producter extends Thread{
	Queue<Integer> queues;
	int maxSize;
	public Producter(Queue<Integer> queues, int maxSize,String name) {
		super();
		this.setName(name);
		this.queues = queues;
		this.maxSize = maxSize;
	}
public void run() {
		super.run();
		while(true){
			synchronized (queues) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(queues.size()>maxSize){
					try {
						System.out.println("队列已满,生产者"+getName()+"等待");
						queues.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
int value = (int)(Math.random()*100);
				queues.offer(value);
				System.out.println("生产者"+getName()+"生产一个元素:"+value);
				queues.notifyAll();
				System.out.println("生产者"+getName()+"退出一次生产过程!");
			}
			
		}
	}
	
	
}

class Customer extends Thread{
	int maxSize;
	Queue<Integer> queues;
	public Customer(int maxSize, Queue<Integer> queues,String name) {
		super();
		setName(name);
		this.maxSize = maxSize;
		this.queues = queues;
	}

@Override
	public void run() {
		super.run();
		while(true){
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			synchronized(queues){
				if(queues.isEmpty()){
					try {
						System.out.println("队列为空,消费者"+getName()+"等待");
						queues.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
System.out.println("消费者"+getName()+"消费一个元素");
				queues.poll();
				queues.notifyAll();
				System.out.println("消费者"+getName()+"退出一次消费过程!");
			}
		}
	}

 

 

参考链接:https://www.cnblogs.com/zhangyinhua/p/7715486.html#_label4

https://www.cnblogs.com/liujinhong/p/6661429.html

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值