diff --git a/src/main/java/thead/thread_2/MyRunnable.java b/src/main/java/thead/thread_2/MyRunnable.java new file mode 100644 index 0000000..5581b4e --- /dev/null +++ b/src/main/java/thead/thread_2/MyRunnable.java @@ -0,0 +1,19 @@ +package thead.thread_2; + +public class MyRunnable implements Runnable { + /** + * When an object implementing interface {@code Runnable} is used + * to create a thread, starting the thread causes the object's + * {@code run} method to be called in that separately executing + * thread. + *

+ * The general contract of the method {@code run} is that it may + * take any action whatsoever. + * + * @see Thread#run() + */ + @Override + public void run() { + System.out.println("运行线程..."); + } +} diff --git a/src/main/java/thead/thread_2/RunnableApplication.java b/src/main/java/thead/thread_2/RunnableApplication.java new file mode 100644 index 0000000..93563b3 --- /dev/null +++ b/src/main/java/thead/thread_2/RunnableApplication.java @@ -0,0 +1,27 @@ +package thead.thread_2; + +public class RunnableApplication { + public static void main(String[] args) { + MyRunnable myRunnable = new MyRunnable(); + Thread thread = new Thread(myRunnable); + thread.start(); + + // 重写方式 + Thread thread2 = new Thread() { + @Override + public void run() { + System.out.println("内部写法"); + } + }; + thread2.setName("t2"); + thread2.start(); + + + // lambda表达式写法 + Runnable runnable = () -> { + System.out.println("Runnable简化写法"); + }; + Thread thread1 = new Thread(runnable); + thread1.start(); + } +}