QUESTION:线程实现的两种方式及匿名内部类实现
ANSWER:
1、多线程两种实现方式
(1)继承Thread
* 定义类继承Thread
* 重写run方法
* 把新线程要做的事写在run方法中
* 创建线程对象
* 开启新线程, 内部会自动执行run方法
public class testThread2 {
public static void main(String []args){
Test t=new Test();
//t.run(); //run()方法是普通方法,要调用start()方法才会开始线程
t.start();
for (int i = 0; i <3000000 ; i++) {
System.out.println("b");
}
}
}
class Test extends Thread{
@Override
public void run() {
for (int i = 0; i <3000000; i++) {
System.out.println("a");
}
}
}
(2)实现Runnable
* 定义类实现Runnable接口
* 实现run方法
* 把新线程要做的事写在run方法中
* 创建自定义的Runnable的子类对象
* 创建Thread对象, 传入Runnable
* 调用start()开启新线程, 内部会自动调用Runnable的run()方法
public class testThread3 {
public static void main(String []args){
Test1 t=new Test1();
Thread td=new Thread(t);
td.start();
for (int i = 0; i <3000000 ; i++) {
System.out.println("b");
}
}
}
class Test1 implements Runnable{
@Override
public void run() {
for (int i = 0; i <300000 ; i++) {
System.out.println("a");
}
}
}
多线程两种实现方式的区别
实现原理:
继承Thread : 由于子类重写了Thread类的run(), 当调用start()时, 直接找子类的run()方法
实现Runnable: 构造函数中传入了Runnable的引用, 成员变量记住了它, start()调用run()方法时内部判断成员变量Runnable的引用是否为空, 不为空编译时看的是Runnable的run(),运行时执行的是子类的run()方法
优缺点:
继承Thread :
好处: 可以直接使用Thread类中的方法,代码简单
弊端: 如果已经有了父类,就不能用这种方法
实现Runnable:
好处: 即使自己定义的线程类有了父类也没关系,因为有了父类也可以实现接口,而且接口是可以多实现的
弊端: 不能直接使用Thread中的方法需要先获取到线程对象后,才能得到Thread的方法,代码复杂
多线程的安全问题及解决方案
问题:当多线程并发, 有多段代码同时执行时,数据会产生错乱。
方案:我们希望某一段代码执行的过程中CPU不要切换到其他线程工作. 这时就需要同步。
public class testThread12 {
public static void main(String []args){
final Print p=new Print();
new Thread(){
@Override
public void run() {
while (true){
p.print1();
}
}
}.start();
new Thread(){
@Override
public void run() {
while (true){
p.print2();
}
}
}.start();
}
}
class Print{
test3 t=new test3();
public void print1(){
//synchronized (new test3())
synchronized (t){ //同步代码块,锁机制,锁对象可以是任意的,不是匿名对象,因为匿名对象地址不一样
System.out.print("传");
System.out.print("智");
System.out.print("播");
System.out.print("客");
System.out.print("\r\n");
}
}
public void print2(){
synchronized (t){
System.out.print("用");
System.out.print("心");
System.out.print("创");
System.out.print("造");
System.out.print("\r\n");
}
}
}
class test3{
}
死锁的产生原理
多线程同步的时候, 如果同步代码嵌套, 使用相同锁, 就有可能出现死锁
更多关于线程的内容请查看本博主博客:
https://blog.csdn.net/yang13563758128/article/details/87261957