首页 > 代码库 > Java学习笔记: 线程的创建
Java学习笔记: 线程的创建
多线程
概念:多线程能够将任务放在不同的线程当中同时处理,可以有效的提高系统的资源使用,尤其针对多处理器对应用的提升更大。
实现理念:把要进行处理的业务逻辑单元放在一个类当中,对于主线程而言就是创建这些类的对象,然后把每个对象作为一个线程进行启动,当中也涉及到多个线程的通信。
实现方法:
一 通过实现Runnable接口
1 构建运行线程的类,该类需要实现Runnable接口,因此需要在该类的定义过程中实现run()方法,这个方法体的内容也就是线程的业务逻辑实现代码
2 创建线程对象
Thread(Runnable threadobj, String threadName)
其中threadobj是创建的线程对象,threadName是可以给线程指定的名称
3 启动线程进行运行
Start();
测试用例:(把不同业务逻辑单元封装在不同的内部类当中,也可以不作为内部类,作为一个通用的逻辑模块供其他对象调用,提高代码的重用率。
package languagelearning.multithread;
public class MultithreadTest {
public class SayHi implements Runnable {
public void run() {
while(true) {
System.out.println("I am thread 1 : Hi");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class SayHello implements Runnable {
public void run() {
while(true) {
System.out.println("I am thread 2 : Hello!");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
MultithreadTest t = new MultithreadTest();
MultithreadTest.SayHi hi = t.new SayHi();
MultithreadTest.SayHello hello = t.new SayHello();
Thread t1 = new Thread(hi, "thread 1");
Thread t2 = new Thread(hello, "thread 2");
t1.start();
t2.start();
while(true) {
System.out.println("main thread ");
Thread.sleep(500);
}
}
}
结果:
dodopipe@T-PC-U:~/workspace/Test/src$ java languagelearning.multithread.MultithreadTest
I am thread 1 : Hi
main thread
I am thread 2 : Hello!
I am thread 1 : Hi
main thread
I am thread 2 : Hello!
I am thread 1 : Hi
main thread
I am thread 2 : Hello!
I am thread 1 : Hi
main thread
I am thread 2 : Hello!
I am thread 1 : Hi
...
二 通过继承Thread类
通过查看JDK当中Thread代码可以了解这个类
1 实现步骤:
覆盖run()方法
调用的时候执行start()方法就行了,
测试用例:
package languagelearning.multithread;
public class MultithreadTest1 extends Thread {
private int number ;
public void run() {
while(true) {
System.out.println("I am thread No. " + number);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public MultithreadTest1(int number) {
this.number = number;
}
public static void main(String[] args) throws InterruptedException {
//create thread
int i = 1;
while (i <= 5) {
(new MultithreadTest1(i)).start();
i++;
}
//hold main thread
while(true) {
System.out.println("I am main thread");
Thread.sleep(1000);
}
}
}
结果:
I am thread No. 2
I am main thread
I am thread No. 5
I am thread No. 3
I am thread No. 4
I am thread No. 1
I am thread No. 2
I am thread No. 5
I am thread No. 3
I am main thread
I am thread No. 4
I am thread No. 1
I am thread No. 2
I am thread No. 5
I am main thread
I am thread No. 4
I am thread No. 3
I am thread No. 1
...
概念:多线程能够将任务放在不同的线程当中同时处理,可以有效的提高系统的资源使用,尤其针对多处理器对应用的提升更大。
实现理念:把要进行处理的业务逻辑单元放在一个类当中,对于主线程而言就是创建这些类的对象,然后把每个对象作为一个线程进行启动,当中也涉及到多个线程的通信。
实现方法:
一 通过实现Runnable接口
1 构建运行线程的类,该类需要实现Runnable接口,因此需要在该类的定义过程中实现run()方法,这个方法体的内容也就是线程的业务逻辑实现代码
2 创建线程对象
Thread(Runnable threadobj, String threadName)
其中threadobj是创建的线程对象,threadName是可以给线程指定的名称
3 启动线程进行运行
Start();
测试用例:(把不同业务逻辑单元封装在不同的内部类当中,也可以不作为内部类,作为一个通用的逻辑模块供其他对象调用,提高代码的重用率。
package languagelearning.multithread;
public class MultithreadTest {
public class SayHi implements Runnable {
public void run() {
while(true) {
System.out.println("I am thread 1 : Hi");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class SayHello implements Runnable {
public void run() {
while(true) {
System.out.println("I am thread 2 : Hello!");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
MultithreadTest t = new MultithreadTest();
MultithreadTest.SayHi hi = t.new SayHi();
MultithreadTest.SayHello hello = t.new SayHello();
Thread t1 = new Thread(hi, "thread 1");
Thread t2 = new Thread(hello, "thread 2");
t1.start();
t2.start();
while(true) {
System.out.println("main thread ");
Thread.sleep(500);
}
}
}
结果:
dodopipe@T-PC-U:~/workspace/Test/src$ java languagelearning.multithread.MultithreadTest
I am thread 1 : Hi
main thread
I am thread 2 : Hello!
I am thread 1 : Hi
main thread
I am thread 2 : Hello!
I am thread 1 : Hi
main thread
I am thread 2 : Hello!
I am thread 1 : Hi
main thread
I am thread 2 : Hello!
I am thread 1 : Hi
...
二 通过继承Thread类
通过查看JDK当中Thread代码可以了解这个类
1 实现步骤:
覆盖run()方法
调用的时候执行start()方法就行了,
测试用例:
package languagelearning.multithread;
public class MultithreadTest1 extends Thread {
private int number ;
public void run() {
while(true) {
System.out.println("I am thread No. " + number);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public MultithreadTest1(int number) {
this.number = number;
}
public static void main(String[] args) throws InterruptedException {
//create thread
int i = 1;
while (i <= 5) {
(new MultithreadTest1(i)).start();
i++;
}
//hold main thread
while(true) {
System.out.println("I am main thread");
Thread.sleep(1000);
}
}
}
结果:
I am thread No. 2
I am main thread
I am thread No. 5
I am thread No. 3
I am thread No. 4
I am thread No. 1
I am thread No. 2
I am thread No. 5
I am thread No. 3
I am main thread
I am thread No. 4
I am thread No. 1
I am thread No. 2
I am thread No. 5
I am main thread
I am thread No. 4
I am thread No. 3
I am thread No. 1
...
Java学习笔记: 线程的创建
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。