1. 创建线程:
创建线程对象的方式一 : 继承Thread类
创建线程对象的方式二: 实现Runnable接口
方式一:继承Thread类
public class MyThread01 extends Thread {
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
System.out.println("Hello ->" +i);
if(i%2==0){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class Demo01 {
//main函数是一个主线程
public static void main(String[] args) throws InterruptedException {
//在主线程中启动一个子线程
//1.创建线程对象
MyThread01 mt01 = new MyThread01();
mt01.setName("子线程01");
//2.启动线程
mt01.start();
//故意在此处停留100毫秒
//Thread.sleep(1000*10);
for (int j = 0; j <10 ; j++) {
System.out.println("hi ->" +j);
if(j%3==0){
Thread.sleep(40);
}
}
}
}方式二: 实现Runnable接口
public class MyRunnable01 implements Runnable {
@Override
public void run() {
for (int i = 0; i <30 ; i++) {
System.out.println(Thread.currentThread().getName()+"->"+i);
if(i%5 == 0 && i>0){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}public class Demo02 {
public static void main(String[] args) {
MyRunnable01 mr01 = new MyRunnable01();
Thread t01 = new Thread(mr01);
t01.start();
}
}构造方法:
public Thread():分配一个新的线程对象。public Thread(String name):分配一个指定名字的新的线程对象。public Thread(Runnable target):分配一个带有指定目标新的线程对象。public Thread(Runnable target,String name):分配一个带有指定目标新的线程对象并指定名字。
常用方法:
- public String getName() :获取当前线程名称。
- public void start() :导致此线程开始执行; Java虚拟机调用此线程的run方法。
- public void run() :此线程要执行的任务在此处定义代码。
- public static void sleep(long millis) :使当前正在执行的线程以指定的毫秒数暂停(暂时停止执行)。
- public static Thread currentThread() :返回对当前正在执行的线程对象的引用。
Thread和Runnable区别:
如果一个类继承Thread,则不适合资源共享。
但是如果实现了Runable接口的话,则很容易的实现资源共享。 实现Runnable接口比继承Thread类所具有的优势:
- 适合多个相同的程序代码的线程去共享同一个资源。
- 可以避免java中的单继承的局限性。
- 增加程序的健壮性,实现解耦操作,代码可以被多个线程共享,代码和线程独立。
- 线程池只能放入实现Runable或Callable类线程,不能直接放入继承Thread的类。
- 在java中,每次程序运行至少启动2个线程。一个是main线程,一个是垃圾收集线程。因为每当使用java命令执行一个类的时候,实际上都会启动一个JVM,每一个JVM其实在就是在操作系统中启动了一个进程。
//线程之间数据共享
public class Demo03 {
public static void main(String[] args) {
MyRunnable02 mr= new MyRunnable02();
Thread t01 = new Thread(mr);
Thread t02 = new Thread(mr);
Thread t03 = new Thread(mr);
t01.start();
t02.start();
t03.start();
}
}
class MyRunnable02 implements Runnable{
private Integer i= 1;
@Override
public void run() {
for (; i <100 ; i++) {
//Thread.currentThread() 获取当前线程对象
//current :当前的
//getName : 获取当前运行的线程对象的名称 , 如果我们没有设置线程对象的名字,那么他们的默认的名字为: Thread-0 , Thread-1 , Thread-2 ....
System.out.println(Thread.currentThread().getName()+"->"+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}2. 线程优先级
setPriority
public class Demo04 {
public static void main(String[] args) {
MyThread04 t1 = new MyThread04("线程A");
MyThread04 t2 = new MyThread04("线程B");
//优先级
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
class MyThread04 extends Thread{
public MyThread04() {
}
public MyThread04(String name) {
super(name);
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+ "-> hello world!");
}
}3. 线程让步
作用是:当前持有CPU时间片资源的线程放弃CPU,让CPU时间片重新分配一次
.yield()
public class Demo05 {
public static void main(String[] args) {
MyThread05 t1 = new MyThread05("A");
MyThread05 t2 = new MyThread05("B");
t1.start();
t2.start();
}
}
class MyThread05 extends Thread{
public MyThread05() {
}
public MyThread05(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
System.out.println(Thread.currentThread().getName()+"->"+i);
if(i>0 && i%4 ==0){
System.out.println(Thread.currentThread().getName() +"->" + "线程让步...");
//线程让步
Thread.yield();
}
}
}
}4. 线程参与
join
public class Demo06 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new MyRunnable06(),"t1");
t1.start();
for (int j = 0; j <10 ; j++) {
System.out.println(Thread.currentThread().getName()+"->"+j);
if(j==5){
//t1.join(); //让t1线程参与进来,直至t1执行完成,主线程再继续
t1.join(1000); //让t1线程参与进来,参与1000毫秒
}else {
Thread.sleep(100);
}
}
}
}
class MyRunnable06 implements Runnable{
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
System.out.println(Thread.currentThread().getName()+"->"+i);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}5. 后台线程
setDaemon
public class Demo15 {
public static void main(String[] args) {
MyRunnable15 mr = new MyRunnable15();
Thread t1 = new Thread(mr,"线程A");
//设置t1线程为后台线程
t1.setDaemon(true);
t1.start();
for(int i = 0 ;i<5 ;i++){
System.out.println(Thread.currentThread().getName()+"->"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyRunnable15 implements Runnable{
@Override
public void run() {
for(int i = 0 ; i<10 ; i++){
System.out.println(Thread.currentThread().getName()+"->"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}版权声明:本文为weixin_44216665原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。