多线程(一):创建多线程的方式以及常用方法
一、多线程的创建方式
1.继承方式
创建步骤:
①创建一个继承于Thread类的类:
package threadExercise;
//第一步
class SubThread extends Thread{
}
public class ThreadTest {
public static void main(String[] args) {
}
}
②重写该类的run方法,实现需要实现的操作:
package threadExercise;
class SubThread extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
}
}
注:此处的currentThread().getName()是返回当前的线程名,后面会介绍一些方法,此处可先行跳过。
③在主类中(此处为ThreadTest)中创建子类的对象,然后调用线程的start()方法。
package threadExercise;
class SubThread extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
SubThread str1 = new SubThread();
str1.start();
}
}
运行结果如下:
也可以增加一个线程,或者在主函数里再定义一个主线程,如下:
package threadExercise;
class SubThread extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class ThreadTest {
public static void main(String[] args) {
SubThread str1 = new SubThread();
SubThread str2 = new SubThread();
str1.start();
str2.start();
//主线程
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
运行结果如下:
扫描二维码关注公众号,回复:
2300829 查看本文章
这是创建多线程的第一种方法,使用该方法,每创建一个多线程时,需要新创建一个子类的对象,并且实现start()方法。
2.实现的方式实现多线程
创建步骤:
①创建一个实现Runnable接口的类,并且重写run()方法:
package threadExercise;
class Thread1 implements Runnable{
@Override
public void run() {
for(int i = 1; i <= 100; i++){
if(i % 3 == 0)
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class ThreadTest2 {
public static void main(String[] args) {
}
}
②在主类中创建一个Runnable接口实现类的对象,然后将此对象作为形参传递给Thread类的构造器中,创建Thread类的对象,此对象即为一个线程:
package threadExercise;
class Thread1 implements Runnable{
@Override
public void run() {
for(int i = 1; i <= 100; i++){
if(i % 3 == 0)
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class ThreadTest2 {
public static void main(String[] args) {
Thread1 thr1 = new Thread1();
Thread thr = new Thread(thr1);
}
}
再调用start方法,即可。
同第一种创建方式,也可以在主函数创建主线程
package threadExercise;
class Thread1 implements Runnable{
@Override
public void run() {
for(int i = 1; i <= 100; i++){
if(i % 3 == 0)
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public class ThreadTest2 {
public static void main(String[] args) {
Thread1 thr1 = new Thread1();
Thread thr = new Thread(thr1);
thr.start();
for (int i = 0; i < 100; i++) {
if(i % 2 == 0)
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
两种创建方式是否又是完美的?将在下节给出答案
二、多线程的常用方法
package thread;
/*
* Thread的常用方法:
* 1.start():启动线程并执行相应的run()方法
* 2.run():子线程要执行的代码放入run()方法中
* 3.currentThread():静态的,调取当前的线程
* 4.getName():获取此线程的名字
* 5.setName():设置此线程的名字
* 6.yield():调用此方法的线程释放当前CPU的执行权
* 7.join():在A线程中调用B线程的join()方法,表示:当执行到此方法,A线程停止执行,直至B线程执行完毕,
* A线程再接着join()之后的代码执行
* 8.isAlive():判断当前线程是否还存活
* 9.sleep(long l):显式的让当前线程睡眠l毫秒
* 10.线程通信:wait() notify() notifyAll()
*
* 设置线程的优先级
* getPriority() :返回线程优先值
setPriority(int newPriority) :改变线程的优先级
*/
class SubThread1 extends Thread {
public void run() {
for (int i = 1; i <= 100; i++) {
// try {
// Thread.currentThread().sleep(1000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getPriority() + ":" + i);
}
}
}
public class TestThread1 {
public static void main(String[] args) {
SubThread1 st1 = new SubThread1();
st1.setName("子线程1");
st1.setPriority(Thread.MAX_PRIORITY);
st1.start();
Thread.currentThread().setName("========主线程");
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().getPriority() + ":" + i);
// if (i % 10 == 0) {
// Thread.currentThread().yield();
// }
// if (i == 20) {
// try {
// st1.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}
System.out.println(st1.isAlive());
}
}