标签:href pre 试题 new t 分配 print code new except
目录
进程:一个应用程序,在进程中会有N多个线程,进程是所有线程的集合。
线程:一条执行路径。
可以提高程序运行的效率。
package com.fly.thread_demo.demo_1;
/**
* 线程创建方式一: 继承Thread类 重写run方法
*/
public class ThreadDemo_1 extends Thread{
@Override
public void run() {
System.out.println("子线程开始");
for (int i = 0; i < 10; i++) {
System.out.println("i = " + i);
}
System.out.println("子线程结束");
}
public static void main(String[] args) {
System.out.println("主线程开始");
Thread t1 = new ThreadDemo_1();
t1.start();
System.out.println("主线程结束");
}
}
package com.fly.thread_demo.demo_1;
/**
* 线程创建方式二: 实现Runnable接口
*/
public class ThreadDemo_2 implements Runnable{
@Override
public void run() {
System.out.println("子线程开始");
for (int i = 0; i < 10; i++) {
System.out.println("i = " + i);
}
System.out.println("子线程结束");
}
public static void main(String[] args) {
System.out.println("主线程开始");
//在Thread构造方法中 接受一个Runnable接口
Thread t2 = new Thread(new ThreadDemo_2());
t2.start();
System.out.println("主线程结束");
}
}
package com.fly.thread_demo.demo_1;
/**
* 线程创建方式三: 匿名类部类
*/
public class ThreadDemo_3 {
public static void main(String[] args) {
System.out.println("主线程开始");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("子线程开始");
for (int i = 0; i < 10; i++) {
System.out.println("i = " + i);
}
System.out.println("子线程结束");
}
});
t3.start();
System.out.println("主线程结束");
}
}
? new Thread();
? 调用start方法后,执行run方法前,等待cpu分配资源
? 执行run方法
package com.fly.thread_demo.demo_1;
/**
* 非守护线程:主线程死亡,不影响 非守护线程
* 守护线程:主线程死亡,守护线程同时死亡
*/
public class ThreadDemo_4 implements Runnable{
@Override
public void run() {
System.out.println("子线程开始");
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("i = " + i);
}
System.out.println("子线程结束");
}
public static void main(String[] args) {
System.out.println("主线程开始");
Thread t4 = new Thread(new ThreadDemo_4());
t4.setDaemon(true);//把t4设置为守护线程 (可以对比 设置和设置的区别)
t4.start();
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程结束");
}
}
package com.fly.thread_demo.demo_1;
/**
* join :
* 1) 线程同步,是并行的线程编程串行,在A线程中调用了B线程的join()方法时,表示只有当B线程执行完毕时,A线程才能继续执行
* 2) 在start方法调用之后调用
* 3) 实现原理:调用线程的wait方法来达到同步的目的
*/
public class ThreadDemo_5 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程i = " + i);
}
}
});
t1.start();
try {
//(可以对比 设置和设置打印结果的区别)
t1.join(); //在主线程中调用t1的join方法 表示让t1先执行完毕再执行主线程
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主i = " + i);
}
}
}
package com.fly.thread_demo.demo_1;
/**
* 面试题 现在有3个线程 t1 ,t2,t3 如何让t1 执行完毕 再执行 t2 t2执行完毕再执行t3
*/
public class ThreadDemo_6 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("t1:i = " + i);
}
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
t1.join();//t2 中 调用 t1.join() :让t1执行完毕执行t2
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("t2:i = " + i);
}
}
});
t2.start();
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
try {
t2.join();//t3中 调用 t2.join() :让t2执行完毕执行t3
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println("t3:i = " + i);
}
}
});
t3.start();
}
}
标签:href pre 试题 new t 分配 print code new except
原文地址:https://www.cnblogs.com/bigfly277/p/10200428.html