第一部分:创建线程的两种方式:
1:继承Thread,并覆写run方法。
public class ThreadDemo extends Thread {
private String name;
public ThreadDemo(String name) {
super(name);
this.name = name;
}
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread " + name + " i now is:" + i);
}
}
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo("thread1");
ThreadDemo thread2 = new ThreadDemo("thread2");
thread1.start();
thread2.start();
System.out.println("main thread finish。");
}
}
2:实现Runnable接口
public class ThreadDemo implements Runnable {
private String name;
public ThreadDemo(String name) {
this.name = name;
}
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread " + name + " i now is:" + i);
}
}
public static void main(String[] args) {
Thread thread1 = new Thread(new ThreadDemo("thread1"));
Thread thread2 = new Thread(new ThreadDemo("thread2"));
thread1.start();
thread2.start();
System.out.println("main thread finish。");
}
}
两个演示执行的结果完全一样,大家可以清除的看到主线程最先执行完毕,然后看到两个子线程交替执行。是的,线程就是让很多任务"同时"执行起来,交替的占用cpu资源,也可以理解为异步的执行多个任务(start方法立即就返回了,不用等待run里面的代码执行完毕)。
现在你已经对java线程有了基本的了解,大家也会发现使用第二种方式实现线程可能会更好,更灵活一些。下面继续。
第二部分:线程的基本控制。
在第一部分的演示中主线程最先执行完毕,显然这不太适合,下面给出修改后的版本。
public class ThreadDemo implements Runnable {
public Thread thread;
public ThreadDemo(String name){
thread=new Thread(this,name);
thread.start();
}
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread "+thread.getName()+" i now is:" + i);
}
}
public static void main(String[] args) throws InterruptedException {
ThreadDemo demo=new ThreadDemo("Test Thread");
demo.thread.join();
System.out.println("main thread finish。");
}
}
现在可以看到是子线程执行完成以后主线程才结束,join方法的意义就把线程加入到当前线程中去,当前线程会等待join的线程执行完成以后才结束。
public class ThreadDemo implements Runnable {
private String name;
public ThreadDemo(String name) {
this.name = name;
}
@Override public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {}
System.out.println("Thread " + name + " i now is:" + i);
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new ThreadDemo("thread1"));
Thread thread2 = new Thread(new ThreadDemo("thread2"));
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("main thread finish。");
}
}
这一次的执行变成两个线程交替加1了,这是因为sleep方法会让线程终止运行指定的毫秒数,将cpu资源让给其它线程,这个方法很好很实用,不是吗?
有了上面的知识,你已经可以使用多线程进行编程了,但是实际应用中多线程是很复杂的。
1:继承Thread,并覆写run方法。
public class ThreadDemo extends Thread {
private String name;
public ThreadDemo(String name) {
super(name);
this.name = name;
}
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread " + name + " i now is:" + i);
}
}
public static void main(String[] args) {
ThreadDemo thread1 = new ThreadDemo("thread1");
ThreadDemo thread2 = new ThreadDemo("thread2");
thread1.start();
thread2.start();
System.out.println("main thread finish。");
}
}
2:实现Runnable接口
public class ThreadDemo implements Runnable {
private String name;
public ThreadDemo(String name) {
this.name = name;
}
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread " + name + " i now is:" + i);
}
}
public static void main(String[] args) {
Thread thread1 = new Thread(new ThreadDemo("thread1"));
Thread thread2 = new Thread(new ThreadDemo("thread2"));
thread1.start();
thread2.start();
System.out.println("main thread finish。");
}
}
两个演示执行的结果完全一样,大家可以清除的看到主线程最先执行完毕,然后看到两个子线程交替执行。是的,线程就是让很多任务"同时"执行起来,交替的占用cpu资源,也可以理解为异步的执行多个任务(start方法立即就返回了,不用等待run里面的代码执行完毕)。
现在你已经对java线程有了基本的了解,大家也会发现使用第二种方式实现线程可能会更好,更灵活一些。下面继续。
第二部分:线程的基本控制。
在第一部分的演示中主线程最先执行完毕,显然这不太适合,下面给出修改后的版本。
public class ThreadDemo implements Runnable {
public Thread thread;
public ThreadDemo(String name){
thread=new Thread(this,name);
thread.start();
}
@Override public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread "+thread.getName()+" i now is:" + i);
}
}
public static void main(String[] args) throws InterruptedException {
ThreadDemo demo=new ThreadDemo("Test Thread");
demo.thread.join();
System.out.println("main thread finish。");
}
}
现在可以看到是子线程执行完成以后主线程才结束,join方法的意义就把线程加入到当前线程中去,当前线程会等待join的线程执行完成以后才结束。
public class ThreadDemo implements Runnable {
private String name;
public ThreadDemo(String name) {
this.name = name;
}
@Override public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {}
System.out.println("Thread " + name + " i now is:" + i);
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(new ThreadDemo("thread1"));
Thread thread2 = new Thread(new ThreadDemo("thread2"));
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("main thread finish。");
}
}
这一次的执行变成两个线程交替加1了,这是因为sleep方法会让线程终止运行指定的毫秒数,将cpu资源让给其它线程,这个方法很好很实用,不是吗?
有了上面的知识,你已经可以使用多线程进行编程了,但是实际应用中多线程是很复杂的。