JSP学习笔记(八十五):Java中的线程
一、通过实现Runnable接口可以创建线程,调用该线程时,会自动执行run()方法,先看一个简单的例子吧:
public class Test1 implements Runnable {
public void run() {
System.out.println(123);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.out.println(456);
}
public static void main(String[] args) {
Runnable r = new Test1();
Thread thread = new Thread(r);
thread.start();
}
}
public void run() {
System.out.println(123);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.out.println(456);
}
public static void main(String[] args) {
Runnable r = new Test1();
Thread thread = new Thread(r);
thread.start();
}
}
执行后,控制台先输出123,过3秒后输出456
二、下面我们改写上面的例子,执行两个线程,并且给线程指定名字:
public class Test2 implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
Runnable r = new Test2();
Thread thread1 = new Thread(r,"name1");
thread1.start();
Thread thread2 = new Thread(r,"name2");
thread2.start();
}
}
public void run() {
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
Runnable r = new Test2();
Thread thread1 = new Thread(r,"name1");
thread1.start();
Thread thread2 = new Thread(r,"name2");
thread2.start();
}
}
这个例子会分别输出name1,name2
三、上面的thread1,thread2是并行执行的,下面再改一下代码,让多线程之间同步执行,所谓的同步执行,就是在thread1执行时,阻塞thread2的执行,等thread1执行完后,再执行thread2:
public class Test3 implements Runnable {
public void execute(String threadName) {
synchronized (this) {
System.out.println(threadName + " start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(threadName + " end");
}
}
public void run() {
String threadName = Thread.currentThread().getName();
execute(threadName);
}
public static void main(String[] args) {
Runnable r = new Test3();
Thread thread1 = new Thread(r, "name1");
thread1.start();
Thread thread2 = new Thread(r, "name2");
thread2.start();
}
}
public void execute(String threadName) {
synchronized (this) {
System.out.println(threadName + " start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(threadName + " end");
}
}
public void run() {
String threadName = Thread.currentThread().getName();
execute(threadName);
}
public static void main(String[] args) {
Runnable r = new Test3();
Thread thread1 = new Thread(r, "name1");
thread1.start();
Thread thread2 = new Thread(r, "name2");
thread2.start();
}
}
可以把synchronized (this) {} 去掉,比较两次执行的效果
四、还可以通过设定线程的优先级,设置线程的执行顺序
public class Test3 implements Runnable {
public void execute(String threadName) {
synchronized (this) {
System.out.println(threadName + " start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(threadName + " end");
}
}
public void run() {
String threadName = Thread.currentThread().getName();
execute(threadName);
}
public static void main(String[] args) {
Runnable r = new Test3();
Thread thread1 = new Thread(r, "name1");
thread1.setPriority(3);
Thread thread2 = new Thread(r, "name2");
thread1.setPriority(2);
thread1.start();
thread2.start();
}
}
public void execute(String threadName) {
synchronized (this) {
System.out.println(threadName + " start");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println(threadName + " end");
}
}
public void run() {
String threadName = Thread.currentThread().getName();
execute(threadName);
}
public static void main(String[] args) {
Runnable r = new Test3();
Thread thread1 = new Thread(r, "name1");
thread1.setPriority(3);
Thread thread2 = new Thread(r, "name2");
thread1.setPriority(2);
thread1.start();
thread2.start();
}
}