一、区别
1、使用区别
public class ThreadDemo01 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " i=" + i);
}
}
public static void main(String[] args) {
ThreadDemo01 threadDemo01 = new ThreadDemo01();
new Thread(threadDemo01).start();
new Thread(threadDemo01).start();
}
}
public class ThreadDemo02 extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(currentThread().getName() + " i=" + i);
}
}
public static void main(String[] args) {
ThreadDemo02 thread_1 = new ThreadDemo02();
thread_1.start();
thread_1.start();
}
}
我们发现使用extends Thread在创建线程的时候如果只new了一个对象,是不可以多个start()启动的。但是Runnable可以。
ThreadDemo02 修改后可以了。
public class ThreadDemo02 extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(currentThread().getName() + " i=" + i);
}
}
public static void main(String[] args) {
ThreadDemo02 thread_1 = new ThreadDemo02();
thread_1.start();
ThreadDemo02 thread_2 = new ThreadDemo02();
thread_2.start();
}
}
上面两种情况都会出现。
2、加上同步代码块
上面初步测试完成,我们加上同步代码块看看结果:
//ThreadDemo01基础上加上同步块,锁this
public class ThreadDemo03 implements Runnable {
@Override
public void run() {
synchronized (this) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " i=" + i);
}
}
}
public static void main(String[] args) {
ThreadDemo03 threadDemo03 = new ThreadDemo03();
new Thread(threadDemo03).start();
new Thread(threadDemo03).start();
}
}
我们发现,使用同一对象 new Thread(threadDemo01).start();出两个线程对this是竞争关系,同步块生效。
//ThreadDemo02基础上加上同步块,锁this
public class ThreadDemo04 extends Thread {
@Override
public void run() {
synchronized (this){
for (int i = 0; i < 10; i++) {
System.out.println(currentThread().getName()+" i=" + i);
}
}
}
public static void main(String[] args) {
ThreadDemo04 thread_1 = new ThreadDemo04();
thread_1.start();
ThreadDemo04 thread_2 = new ThreadDemo04();
thread_2.start();
}
}
可以看出,对 synchronized (this)来说,确实不同对象的创建出的线程是没有竞争关系的。
所以,使用this作为 synchronized代码块竞争资源,使用Runnable要好些。使用Thread必须指定竞争资源。
我在把 synchronized放for循环里面,我们再看看
//ThreadDemo01基础上加上同步块,锁this,synchronized放for循环里
public class ThreadDemo05 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (this){
System.out.println(Thread.currentThread().getName()+" i=" + i);
}
}
}
public static void main(String[] args) {
ThreadDemo05 threadDemo05 = new ThreadDemo05();
new Thread(threadDemo05).start();
new Thread(threadDemo05).start();
}
}
这个就好比是
public class ThreadDemo06 extends Thread {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (this){
System.out.println(currentThread().getName()+" i=" + i);
}
}
}
public static void main(String[] args) {
ThreadDemo06 thread_1 = new ThreadDemo06();
thread_1.start();
ThreadDemo06 thread_2 = new ThreadDemo06();
thread_2.start();
}
}
由上可以看出,将同步块放for循环里,也就是每次for循环执行一次,就会锁一次资源,那么Thread虽然是两个对象,但是效果其实和Runnable 一个对象是一样的,两个线程共20次for,每一次其是相对来讲都是独立的,谁抢到谁执行,之间也没有同步先后关系。
3、使用同步方法
//Runnable使用同步方法
public class ThreadDemo07 implements Runnable {
@Override
public void run() {
sout();
}
synchronized void sout(){
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+" i=" + i);
}
}
public static void main(String[] args) {
ThreadDemo07 threadDemo07 = new ThreadDemo07();
new Thread(threadDemo07).start();
new Thread(threadDemo07).start();
}
}
public class ThreadDemo08 extends Thread {
@Override
public void run() {
sout();
}
synchronized void sout(){
for (int i = 0; i < 10; i++) {
System.out.println(currentThread().getName()+" i=" + i);
}
}
public static void main(String[] args) {
ThreadDemo08 thread_1 = new ThreadDemo08();
thread_1.start();
ThreadDemo08 thread_2 = new ThreadDemo08();
thread_2.start();
}
}
我们看到,因为Runnable 是使用的一个对象 threadDemo01,不管是同步方法和同步块,两个线程都是有竞争关系的,也就是,一个线程抢到锁,只有等他的for循环执行完以后释放锁后,另外一个线程才能拿到锁。
而Thread因为没办法使用同一个对象去创建多个线程,所以两个线程在同步块下锁this,和同步方法中都没有竞争关系,都是各自执行各自的。如果我们新建一个对象
再试试,不锁this,锁一个对象:
public class ThreadDemo09 implements Runnable {
static final Object obj = new Object();
@Override
public void run() {
synchronized (obj) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " i=" + i);
}
}
}
public static void main(String[] args) {
ThreadDemo09 threadDemo09 = new ThreadDemo09();
new Thread(threadDemo09).start();
new Thread(threadDemo09).start();
}
}
}
public class ThreadDemo10 extends Thread {
static final Object obj = new Object();
@Override
public void run() {
synchronized (obj) {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + " i=" + i);
}
}
}
public static void main(String[] args) {
ThreadDemo10 thread_1 = new ThreadDemo10();
thread_1.start();
ThreadDemo10 thread_2 = new ThreadDemo10();
thread_2.start();
}
}
public class ThreadDemo11 implements Runnable {
static final Object obj = new Object();
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (obj) {
System.out.println(Thread.currentThread().getName() + " i=" + i);
}
}
}
public static void main(String[] args) {
ThreadDemo11 threadDemo11 = new ThreadDemo11();
new Thread(threadDemo11).start();
new Thread(threadDemo11).start();
}
}
public class ThreadDemo12 extends Thread {
static final Object obj = new Object();
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (obj) {
System.out.println(Thread.currentThread().getName() + " i=" + i);
}
}
}
public static void main(String[] args) {
ThreadDemo12 thread_1 = new ThreadDemo12();
thread_1.start();
ThreadDemo12 thread_2 = new ThreadDemo12();
thread_2.start();
}
}
也可能这样:
总结:
1、创建线程extends Thread方式,一个对象,只能创建一个线程。implements Runnable方式,一个对象,可以多次start()。
2、synchronized关键字,如果锁this,实质与线程创建的对象有关,同一个对象,就形成竞争,不同对象,各自执行各自,线程间无竞争。如果增加一个竞争对象去锁定,那创建的线程之间的竞争关系就与创建的对象无关了,只与这个竞争对象有关,使用哪个方式创建线程也就无所谓了。
3、synchronized使用在方法上,等于就是锁this方法块。
4、synchronized每锁一次,释放后,cpu的使用会释放出来,其他线程就可以抢占cpu。
5、当修饰静态方法的时候,锁定的是当前类的Class对象,也就是锁类;当修饰非静态方法的时候,锁定的是当前实例对象this。
最后,留下一个例子,理解了应该就懂了
public class test01 {
static class T1 implements Runnable {
private int a = 0;
@Override
public void run() {
while (a < 10)
synchronized (this) {
System.out.println(Thread.currentThread().getName() + " I=" + a++);
}
}
}
static class T1_1 extends Thread {
private int a = 0;
@Override
public void run() {
while (a < 10)
synchronized (this) {
System.out.println(Thread.currentThread().getName() + " I=" + a++);
}
}
}
static class T2 implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
synchronized (this) {
System.out.println(Thread.currentThread().getName() + " I=" + i);
}
}
}
}
public static void main(String[] args) {
// //三个线程合力从零加到10(有线程安全问题)
// T1 t1 = new T1();
// new Thread(t1).start();
// new Thread(t1).start();
// new Thread(t1).start();
//三个线程每个线程任务是从0加到10
// T1_1 t1 = new T1_1();
// t1.start();
// T1_1 t2 = new T1_1();
// t2.start();
// T1_1 t3 = new T1_1();
// t3.start();
//每个线程各自循环10次,从0加到10
T2 t2 = new T2();
new Thread(t2).start();
new Thread(t2).start();
new Thread(t2).start();
}
}