static class MyThread1 implements Runnable {
@Override
public void run() {
System.out.println("MyThread1");
}
}
public static void main(String[] args) {
new Thread(new MyThread1()).start();
System.out.println("运行结束");
}
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName());
}
static class MyThread extends Thread {
public MyThread() {
System.out.println("构造方法打印:" + Thread.currentThread().getName());
}
@Override
public void run() {
System.out.println("run方法打印:" + Thread.currentThread().getName());
}
}
public static void main(String[] args) {
Thread t1 = new MyThread();
t1.start();
}
构造方法打印:main
run方法打印:Thread-0
static class MyThread extends Thread {
public MyThread() {
System.out.println("构造方法打印:" + Thread.currentThread().getName());
}
@Override
public void run() {
System.out.println("run方法打印:" + Thread.currentThread().getName());
}
}
static class MyThread1 implements Runnable {
@Override
public void run() {
System.out.println("MyThread1");
}
}
public static void main(String[] args) {
Thread t1 = new MyThread();
// t1.start();
t1.run();
}
构造方法打印:main
run方法打印:main
static class MyThread extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
int count = 0;
for (int i = 0; i < 5000000; i++) {
Thread.yield();
count = count + (i + 1);
}
long endTime = System.currentTimeMillis();
System.out.println("用时:" + (endTime - beginTime));
}
}
public static void main(String[] args) {
Thread t1 = new MyThread();
t1.start();
}
/**
* The minimum priority that a thread can have.
*/
public final static int MIN_PRIORITY = 1;
/**
* The default priority that is assigned to a thread.
*/
public final static int NORM_PRIORITY = 5;
/**
* The maximum priority that a thread can have.
*/
public final static int MAX_PRIORITY = 10;