java--Object

1 Object作为所有类的父类,感觉有必要对其好好研究一下。Object位于java.lang;包下。其中的方法为:
这里写图片描述
1 getClass()方法;
在Object定义为final,不可被重写,返回调用该对象对应的Class对象。

Integer a=1;
		System.out.println(a.getClass());

这里写图片描述
2 hashCode()方法:
hashCode返回的是一个不仅仅包含 存 储 地 址 的 散 列 整 数 \color{#FF0000}{存储地址的散列整数}
两 个 对 象 可 能 得 到 相 同 的 h a s h C o d e 值 。 相 同 的 对 象 一 定 得 到 的 是 相 同 的 h a s h C o d e 值 。 \color{#FF0000}{两个对象可能得到相同的hashCode值。相同的对象一定得到的是相同的hashCode值。} hashCodehashCode

那么hashCode有啥用?
在涉及到散列值的集合容器中,比如HashMap,hashCode主要用来证明在数组中是否可能存中对象,
因为不同的hashCode值对应的对象肯定是不同的。
需要进一步确认是否是真的是同一个对象,那么就需要equals方法,hashCode值能进一步缩小比较的空间。
重写HashCode、equals方法有一条通用的原则,

1 e q u a l s 返 回 为 t r u e 的 两 个 对 象 , 对 应 的 h a s h C o d e 值 一 定 是 相 同 的 。 \color{#FF0000}{ 1 equals返回为true的两个对象,对应的hashCode值一定是相同的。 } 1equalstruehashCode
2 e q u a l s 返 回 的 f a l s e 的 两 个 对 象 , 对 应 的 h a s h C o d e 值 不 要 钱 相 同 的 。 \color{#FF0000}{ 2 equals返回的false的两个对象,对应的hashCode值不要钱相同的。} 2equalsfalsehashCode
3 h a s h C o d e 值 相 同 的 两 个 对 象 , e q u a l s 可 以 返 回 f a l s e 。 \color{#FF0000}{ 3 hashCode值相同的两个对象,equals可以返回false。} 3hashCodeequalsfalse
4 h a s h C o d e 值 不 相 同 的 两 个 对 象 , e q u a l s 必 须 要 返 回 f a l s e 。 \color{#FF0000}{ 4 hashCode值不相同的两个对象,equals必须要返回false。 } 4hashCodeequalsfalse

参考博客:
http://blog.csdn.net/fenglibing/article/details/8905007
https://www.cnblogs.com/dolphin0520/p/3681042.html

3 equals()方法:
源码:

public boolean equals(Object obj) {
		return this == obj;
	}

Object中比较的是在内存中的地址。。
例:这里写图片描述

  • clone方法
    详见https://blog.csdn.net/BtWangZhi/article/details/99683383

  • toString()方法:

public String toString() {
		return (new StringBuilder()).append(getClass().getName()).append("@")
				.append(Integer.toHexString(hashCode())).toString();
	}

将hashCode方法得到的散列码转为16进制。步骤如下
1 先将int类型的整数转为二进制表示。
2 然后每4位为一组转为16进制,结果串起来即为该数的16进制数。
转换的核心逻辑在Integer.toUnsignedString0中。
参考
https://blog.csdn.net/lianjiww/article/details/90183672

5 notify();
使用案例

	@Test
	public void test04() throws InterruptedException {
		Object obj = new Object();

		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				synchronized (obj) {
					System.out.println(Thread.currentThread().getName() 
							+ "-获取obj同步锁成功");
					try {
						Thread.sleep(1000);

						System.out.println(Thread.currentThread().getName() 
								+ "-释放同步锁");
						obj.wait();

						System.out.println(Thread.currentThread().getName() 
								+ "-再次获取obj同步锁成功");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		});
		t1.setName("t1");
		t1.start();

		Thread.sleep(2000);

		synchronized (obj) {
			System.out.println(Thread.currentThread().getName() 
					+ "-获取obj同步锁成功");
			obj.notify();
			
			System.out.println("完成唤醒,陷入睡眠");
			Thread.sleep(2000);
			
			System.out.println(Thread.currentThread().getName() 
					+ "-释放同步锁");
		}		
		while(Thread.activeCount()>2) {}
	}
Wakes up a single thread that is waiting on this object's monitor. 
If any threads are waiting on this object, one of them is chosen to be awakened. 
The choice is arbitrary and occurs at the discretion of the implementation. 
A thread waits on an object's monitor by calling one of the wait methods. 

唤醒单个等待在该对象同步临界区的线程,如果有多个线程在等待,
其中一个被选中唤醒,选择是随机的,是有执行者决定的。

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.
 The awakened thread will compete in the usual manner with any other threads 
 that might be actively competing to synchronize on this object; 
 for example, the awakened thread enjoys no reliable privilege or disadvantage 
 in being the next thread to lock this object. 

唤 醒 的 线 程 不 会 执 行 直 到 当 前 线 程 丢 弃 对 象 上 的 锁 。 \color{#FF0000}{唤醒的线程不会执行直到当前线程丢弃对象上的锁。} 线线
唤醒线程将会和其他活跃的线程公平竞争对象的同步,
例如,唤醒的线程在下一个获取对象锁是不会有特权或者不公平的。

This method should only be called by a thread that is the owner of this object's monitor. 
A thread becomes the owner of the object's monitor in one of three ways: 
•By executing a synchronized instance method of that object. 
•By executing the body of a synchronized statement that synchronizes on the object. 
•For objects of type Class, by executing a synchronized static method of that class. 

Only one thread at a time can own an object's monitor.

这 个 方 法 只 有 可 能 会 获 取 对 象 同 步 临 界 区 的 线 程 才 会 调 用 , \color{#FF0000}{这个方法只有可能会获取对象同步临界区的线程才会调用,} 线
一个线程获取对象同步临界区通过如下三种方式。

  • 通过执行对象的同步实例方法。
  • 执行对象的同步代码块。
  • 如果object是类对象,执行类对象的静态方法。
    在同一时间只有一个线程能获取对象的监视器(同步临界区)

6 notifyAll();
唤醒在此同步监视器上等待的所有线程。只有当前线程放弃对该同步监视器的锁定才可以执行被唤醒的线程。

7 wait(long l);

Causes the current thread to wait until either another 
thread invokes the java.lang.Object.notify() method 
or the java.lang.Object.notifyAll() method for this object, 
or a specified amount of time has elapsed. 

The current thread must own this object's monitor. 

让当前线程等待知道另外一个线程执行notify方法或者notifyAll方法,获取时间已经过去。

This method causes the current thread (call it T) to place itself in the wait set for this object 
and then to relinquish any and all synchronization claims on this object. 
Thread T becomes disabled for thread scheduling purposes 
and lies dormant until one of four things happens: 
•Some other thread invokes the notify method for this object 
and thread T happens to be arbitrarily chosen as the thread to be awakened. 
•Some other thread invokes the notifyAll method for this object. 
•Some other thread interrupts thread T. 
•The specified amount of real time has elapsed, more or less. 
If timeout is zero, however, then real time is not taken into consideration 
and the thread simply waits until notified. 

这个方法导致当前线程添加它自己到等待集合中,并且丢弃同步临界区信息,线程将会关闭,不会被线程调度器调度。
陷入休眠知道如下四种事情发生。

  • 一些线程调用了notify方法。并且线程立刻选择,然后被唤醒。
  • 其他线程执行了notifyAll方法
  • 有一些线程中断了当前线程。
  • 等待的时间过期了。如果时间为0,那么时间不会被用来判断,线程陷入等待,直到被唤醒
The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. 
It then competes in the usual manner with other threads for the right to synchronize on the object; 
once it has gained control of the object, all its synchronization claims on the object are restored 
to the status quo ante - that is, to the situation as of the time that the wait method was invoked. 
Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, 
the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked. 
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. 
While this will rarely occur in practice, applications must guard against it by testing for the condition 
that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied.

一个线程从等待集合种异常,重新进入线程调度中,那么会和其他线程竞争对象的同步临界区。
一 旦 获 取 到 , 所 有 同 步 o b j e c t 信 息 都 会 归 还 到 当 前 线 程 , 包 括 当 前 执 行 w a i t 方 法 时 的 情 形 \color{#FF0000}{一旦获取到,所有同步object信息都会归还到当前线程,包括当前执行wait方法时的情形} object线wait
那么线程从wait方法返回(只是wait执行完,继续向后执行)。于是,wait方法返回,对象的同步状态、线程
和当时执行wait时一摸一样。线程也可以在不会唤醒、中断、超时的情况下醒来,这是一种所谓的虚假唤醒。
在实际中很少发生,虚拟机一定重新通过测试线程唤醒检查,继续等待如果条件不满足。

Note that the wait method, as it places the current thread into the wait set for this object,
 unlocks only this object; any other objects on which the current thread 
 may be synchronized remain locked while the thread waits. 

This method should only be called by a thread that is the owner of this object's monitor. 
See the notify method for a description of the ways 
in which a thread can become the owner of a monitor.

注意,当前线程执行wait方法,将会把当前线程添加到object等待集合中,释放object锁,
当前线程获取的其他objects会持有同步锁,
通 俗 点 说 就 是 如 果 嵌 套 了 两 层 s y n c h r o n i z e d , 里 层 执 行 w a i t 方 法 , \color{#FF0000}{通俗点说就是如果嵌套了两层synchronized ,里层执行wait方法,} synchronizedwait
只 会 释 放 里 面 一 层 o b j e c t 的 锁 , 不 会 释 放 外 层 的 \color{#FF0000}{只会释放里面一层object的锁,不会释放外层的} object

8 finalize()方法;
源码:

protected void finalize() throws Throwable {
	}

在垃圾回收机制回收任何对象之前,总会先调用它的finalize()方法,该方法可使该对象重新复活,
从而导致垃圾回收机制取消回收。或者一个对象中存在一个socket了解,可以在finalize方法中关闭连接。
https://www.cnblogs.com/ktao/p/8586966.html
垃圾回收方式有两种方式:
1 调用System类的gc()静态方法:System.getRuntime().gc()
2 调用Runtime.getRuntime().gc();
该回收机制只是提示系统垃圾回收,系统完全有可能置之不理。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值