java基础-多线程的创建

Thread:A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. 

多线程的创建和启动
1.Java语言的JVM允许程序运行多个线程,它通过Java.lang.Thread类来实现
2.Threaad类的特性
①每个线程通过特定的Thread对象的run()方法来完成操作的,经常把run()方法的主题称为线程体
②同过Thread对象的start()方法来调用这个线程

There are two ways to create a new thread of execution. 

线程创建的两种方法

One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. An instance of the subclass can then be allocated and started. For example, a thread that computes primes larger than a stated value could be written as follows: 
  通过继承的方式创建多线程

 class PrimeThread extends Thread {
         long minPrime;
         PrimeThread(long minPrime) {
             this.minPrime = minPrime;
         }


         public void run() {
             // compute primes larger than minPrime
              . . .
         }
     }
The following code would then create a thread and start it running: 




     PrimeThread p = new PrimeThread(143);

     p.start();

例子:

此处插入代码TestThread1

package com.atguigu.java1;
/**
 * 创建一个子线程,完成1-100之间自然数的输出。同样的,主线程执行同样的操作
 * 创建多线程的第一种方式:继承java.lang.Thread类
 *
 */
//1.创建一个继承Thread的子类
class NumThread extends Thread{
	//2.重写Thread类的run()方法,方法内实现此子线程的功能
	public void run(){
		for(int i=0;i<=100;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}
}
public class TestThread1 {
	
	public static void main(String[] args) {
		//3.创建一个子类的对象
		NumThread n1=new NumThread();
		NumThread n2=new NumThread();
		n1.setName("子线程1");
		n2.setName("子线程2");
		
		//调用线程的start()方法,启动此线程,调用相应的run()方法,
		//一个线程只能执行一次Start()
		//不能通过Thread实现类的run()方法启动一个线程
		n1.start();
		n2.start();
		Thread.currentThread().setName("主线程");
		for(int i=0;i<=100;i++){
			System.out.println(Thread.currentThread().getName()+":"+i);
			
		}
	}
}

此处插入代码TestThread2

package com.atguigu.java1;
/**
 * 创建两个子线程,让其中一个输出1-100之间的偶数,另一个输出1-100之间的奇数
 * 
 *
 */
class SubThread1 extends Thread {
	public void run() {
		for (int i = 0; i <= 100; i++) {
			if (i % 2 == 0) {
				System.out.println(Thread.currentThread().getName() + ":" + i);
			}
		}
	}
}

class SubThread2 extends Thread {
	public void run() {
		for (int i = 0; i <= 100; i++) {
			if (i % 2 == 1) {
				System.out.println(Thread.currentThread().getName() + ":" + i);
			}
		}
	}
}

public class TestThread2 {

	public static void main(String[] args) {
		SubThread1 s1 = new SubThread1();
		SubThread2 s2 = new SubThread2();
		s1.setName("子线程1");
		s2.setName("子线程2");
		s1.start();
		s2.start();
		// 通过匿名内部类的方法来写本题
		/*new Thread() {
			public void run() {
				for (int i = 0; i <= 100; i++) {
					if (i % 2 == 0) {
						System.out.println(Thread.currentThread().getName() + ":" + i);
					}
				}
			}
		}.start();
		new Thread() {
			public void run() {
				for (int i = 0; i <= 100; i++) {
					if (i % 2 == 1) {
						System.out.println(Thread.currentThread().getName() + ":" + i);
					}
				}
			}
		}.start();*/
	}
}

The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. The same example in this other style looks like the following: 
 通过实现的方式创建多线程
  class PrimeRun implements Runnable {
         long minPrime;
         PrimeRun(long minPrime) {
             this.minPrime = minPrime;
         }


         public void run() {
             // compute primes larger than minPrime
              . . .
         }
     }
 The following code would then create a thread and start it running: 




     PrimeRun p = new PrimeRun(143);

     new Thread(p).start();

例子:

此处插入TestThread3

/**
 * 创建两个子线程,让其中一个输出1-100之间的偶数,另一个输出1-100之间的奇数
 * 实现的方式
 */										
//创建一个实现Runable接口的类
class PrintNum1 implements Runnable{

	@Override
	//实现接口的抽象方法
	public void run() {
		// TODO Auto-generated method stub
		for (int i = 0; i <= 100; i++) {
			if (i % 2 == 0) {
				System.out.println(Thread.currentThread().getName() + ":" + i);
			}
		}
	}
	
}
public class TestThread3 {
	public static void main(String[] args) {
		//3.创建一个Runable接口实现类的 对象
		PrintNum1 p=new PrintNum1();
		//将此对象作为形参传递给Thread类的构造器中,创建Thread类的对象,此对象即为一个线程
		Thread t1=new Thread(p);
		Thread t2=new Thread(p);
		//5.调用start()方法,启动线程冰调用run()方法
		t1.start();//启动线程,执行Thread对象生成时构造器形参的对象的run()方法
		t2.start();
	}
}

  继承创建多线程VS实现创建多线程
 *1.联系 public class Thread implements Runnable
 *2.实现的方式优于继承的方式
 * 原因:①实现的方式避免了Java中单继承的局限性
 * ②如果多个线程操作同一分资源,更适合使用实现的方式
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值