线程的创建和使用
下面这个示例代码, 不是 多线程
public class Sample {
public void method1(String str) {
System.out.println(str);
}
public void method2(String str) {
method(str);
}
public static void main(String[] args) {
Sample s = new Sample();
s.method2("hello!");
}
}
There are two ways to create a new thread of execution.
1. 继承Thread
jdk1.0就有
One is to declare a class to be a subclass of Thread.
This subclass should override the run method of class Thread.
An instance of the subclass can then be allocated and started.
For example, a thread that computes primes larger than a stated value could be written as follows:
步骤
1, 创建一个继承于 Thread 的子类
2, 重写 Thread类的 run()
3, 创建 Thread类 的 子类的对象
4, 通过此对象调用 start()
例子: 遍历100以内的所有的偶数
// 1.创建一个继承于 Thread 的子类
class MyThread extends Thread {
// 2.重写 Thread类的 run()
// 重写的run()中写下 想让这个 thread 做的事情
@Override
public void run() {
for(int i = 0; i < 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
}
public class ThreadTest {
public static void main(String[] args) {
// 3.创建Thread类的子类的对象
MyThread t1 = new MyThread();
// 4.通过次对象调用start()
// 作用: 1.启动当前线程 2.调用当前线程的run()
t1.start();
for (int i = 0; i < 100; i++) {
if (i % 2 != 0) {
System.out.println(i + "***********");
}
}
}
}
java.lang.Thread部分源码
public class Thread implements Runnable {
public static native Thread currentThread();
// 构造器
public Thread() {}
public Thread(Runnable target) {}
public Thread(Runnable target, AccessControlContext acc) {}
public Thread(ThreadGroup group, Runnable target) {}
public Thread(String name) {}
public Thread(ThreadGroup group, String name) {}
public Thread(Runnable target, String name) {}
public Thread(ThreadGroup group, Runnable target, String name) {}
public Thread(ThreadGroup group, Runnable target, String name, long stackSize) {}
public synchronized void start() {}
@Override
public void run() {}
public final synchronized void setName(String name) {}
public final String getName() {}
public static native void yield();
public final synchronized void join(long millis) {}
public final synchronized void join(long millis, int nanos) throws InterruptedException {}
public final void join() throws InterruptedException {}
public static native void sleep(long millis) throws InterruptedException;
@Deprecated
public final void stop() {}
@Deprecated
public final synchronized void stop(Throwable obj) {}
public final native boolean isAlive() {}
}
2. 实现Runnable接口
jdk1.0就有
jdk8文档中, 还是写的2种方法,
jdk5.0已经新增了2种方法, 文档忘了改
https://www.bilibili.com/video/BV1Kb411W75N?p=424
1, 创建一个实现 Runnable接口 的类
2, 实现类 去实现 Runnable中的 抽象方法: run()
3, 创建实现类的对象
4, 将此对象作为参数传递到 Thread类 的构造器中, 创建 Thread类 的对象
5, 通过 Thread类 的对象, 调用 start()
// 1, 创建一个实现 Runnable接口 的类
class MyThread implements Runnable {
// 2, 实现类 去实现 Runnable中的 抽象方法: run()
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}
}
}
public class ThreadTest {
public static void main(String[] args) {
// 3, 创建实现类的对象
MyThread myThread = new MyThread();
// 4, 将此对象作为参数传递到 Thread类 的构造器中, 创建 Thread类 的对象
Thread t1 = new Thread(myThread);
// 5, 通过 Thread类 的对象, 调用 start()
t1.start();
}
}
java.lang.Thread部分源码
public class Thread implements Runnable {
private Runnable target;
// 构造器
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
@Override
public void run() {
if (target != null) {
target.run();
}
}
}
3. holable接口
https://www.bilibili.com/video/BV1Kb411W75N?p=424
jdk5.0新增
4. 线程池
https://www.bilibili.com/video/BV1Kb411W75N?p=424
jdk5.0新增
参考链接
https://www.bilibili.com/video/BV1Kb411W75N?p=418
https://www.bilibili.com/video/BV1Kb411W75N?p=419
https://www.bilibili.com/video/BV1Kb411W75N?p=424
https://docs.oracle.com/javase/8/docs/api/index.html