1.线程创建
方法一:继承Thread类,重写run方法
class MyThread extends Thread {
@Override
public void run() {
System.out.println("hello thread");
}
}
public class ThreadDemo8 {
public static void main(String[] args) {
Thread t = new MyThread();
t.start();
}
}
方法二:实现Runnable类,重写run方法
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("hello thread");
}
}
public class ThreadDemo8 {
public static void main(String[] args) {
Runnable runnable = new MyRunnable();
Thread t = new Thread(runnable);
t.start();
}
}
方法三:和方法一一样,不过用内部类实现
public class ThreadDemo8 {
public static void main(String[] args) {
Thread t = new Thread() {
@Override
public void run() {
System.out.println("hello thread");
}
};
t.start();
}
}
方法四:和方法二一样,也是内部类实现
public class ThreadDemo8 {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello thread");
}
});
t.start();
}
}
方法五:lambda方法实现
public class ThreadDemo8 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println("hello thread");
});
t.start();
}
}
2.线程中断
方法一:使用标志位来控制线程是否要停止
public class ThreadDemo8 {
public static boolean flag = true;
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
while (flag) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
Thread.sleep(5000);
flag = false;
}
}
方法二:使用Thread自带的标志位来进行判定
public class ThreadDemo8 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
break;
}
}
});
t.start();
Thread.sleep(5000);
t.interrupt();
}
}
3.线程等待
public class ThreadDemo8 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
for (int i = 0; i < 10; i++) {
System.out.println("线程t在执行");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程t执行完");
});
t.start();
//主线程等待t线程执行完
t.join();
System.out.println("主线程执行完");
}
}
4.线程休眠
public class ThreadDemo8 {
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
Thread.sleep(3 * 1000);
long end = System.currentTimeMillis();
System.out.println("主线程休眠了" + ((end - start) / 1000) + "秒");
}
}
5.线程实例
public class ThreadDemo8 {
public static void main(String[] args) throws InterruptedException {
serial();
concurrency();
}
public static void concurrency() throws InterruptedException {
long begin = System.currentTimeMillis();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
int a = 0;
for (long i = 0; i < 10_0000_0000; i++) {
a--;
}
}
});
thread.start();
int b = 0;
for (long i = 0; i < 10_0000_0000; i++) {
b--;
}
thread.join();
long end = System.currentTimeMillis();
double ms = (end - begin) * 1.0;
System.out.printf("并发:%f 毫秒%n", ms);
}
private static void serial() {
long begin = System.currentTimeMillis();
int a = 0;
for (long i = 0; i < 10_0000_0000; i++) {
a--;
}
int b = 0;
for (long i = 0; i < 10_0000_0000; i++) {
b--;
}
long end = System.currentTimeMillis();
double ms = (end - begin) * 1.0;
System.out.printf("串行:%f 毫秒%n", ms);
}
}