package Work4;
public class Cat extends Thread{
/*一定要重写Thread类里面的Run方法
因为不写的话默认就是调用父类型特征区里面的run方法
我们创建并启动线程,是需要写自己独有的业务逻辑,因此就必须重写它
这个地方不重写也不会报错,因此需要特别关注一下
*/
@Override
public void run() {
int count=0;
while(count<10){
System.out.println("线程名"+Thread.currentThread().getName()+"的小猫咪正在喵喵喵"+(++count));
}
}
}
package Work4;
public class ThreadTest {
public static void main(String[] args) {
Cat cat=new Cat();
cat.start();
}
}

package Work4;
public class Cat implements Runnable{
@Override
public void run() {
int count=0;
while(count<10){
System.out.println("线程名"+Thread.currentThread().getName()+"的小猫咪正在喵喵喵"+(++count));
}
}
}
package Work4;
public class ThreadTest {
public static void main(String[] args) {
//实例化自定义类,该类实现了Runnable接口
Cat cat=new Cat();
//实例化Thread类(线程类),在实例化的过程中将cat对象传入进去
Thread thread = new Thread(cat);
thread.start();
}
}

package Work4;
public class ThreadTest {
public static void main(String[] args) {
//采用匿名内部类的方式
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
int count=0;
while(count<10){
System.out.println("线程名"+Thread.currentThread().getName()+"的小猫咪正在喵喵喵"+(++count));
}
}
});
thread.start();
}
}

不能,去调用run方法的过程中本质上就是调用一个普通方法,启动线程的方法是start方法,他在底层调用了start0方法(启动线程的关键方法)
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
/* Notify the group that this thread is about to be started
* so that it can be added to the group's list of threads
* and the group's unstarted count can be decremented. */
group.add(this);
boolean started = false;
try {
start0();
//本质是这个方法启动了线程
started = true;
} finally {
try {
if (!started) {
group.threadStartFailed(this);
}
} catch (Throwable ignore) {
/* do nothing. If start0 threw a Throwable then
it will be passed up the call stack */
}
}
}
private native void start0();
//被native修饰的方法代表是用c/c++实现的代码