静态文件 程序 启动以后以 进程方式在 操作系统驻留
总结 :
eg :
概念 :
时间片(cpu 一段的执行时间 以 纳秒 为单位)
时间片 分配给 某个线程 ----> 某个线程 来执行
这种情况 叫并发执行。
这就有了 并行 执行
package nuc.zm.MoreThread.create_thread;
import nuc.zm.io.ObjectIOTest.Object;
/**
* demo1
* @description 创建线程的方式 1
* @author zm
* @date 2022/10/15
*/
public class Demo1 {
public static void main(String[] args) {
TestThread testThread = new TestThread();
for (int i = 0; i < 10; i++) {
System.out.println("不会出现争夺资源的main" + i);
}
Thread thread = Thread.currentThread();
thread.setName("主线程");
// 让子线程先启动 才会出现 和主线程抢夺资源的情况hh
testThread.setName("子线程");
testThread.start();
for (int i = 0; i < 10; i++) {
System.out.println(thread.getName() + i);
}
}
static class TestThread extends Thread {
/**
* 运行
* 线程对象 争抢资源的任务 必须放在 run 方法中
*/
@Override
public void run() {
super.run();
for (int i = 0; i < 10; i++) {
System.out.println(getName() + " ==== " + i);
}
}
}
}
package nuc.zm.MoreThread.create_thread;
/**
* 以及接下来
* 实现 Runnable 接口 成为线程类
* @author zm
* @date 2022/10/16
*/
public class Demo2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "====" + i);
}
}
// main 线程 启动器
public static void main(String[] args) {
Demo2 demo2 = new Demo2();
Thread thread = new Thread(demo2);
thread.setName("子线程~~");
thread.start();
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "========" + i);
}
}
}
package nuc.zm.MoreThread.create_thread;
import java.util.Random;
import java.util.concurrent.*;
/**
* 可调用测试
*
* @author zm
* @date 2022/11/06
*/
public class CallableTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ThreadTest threadTest = new ThreadTest();
threadTest.start();
}
static class ThreadTest implements Callable<Integer> {
private String name;
@Override
public Integer call() throws Exception {
int speed = new Random().nextInt(10);
int result = 0;
for (int i = 1; i < 10; i++) {
Thread.sleep(1000);
result = i * speed;
System.out.println("第 " + i + " 秒 " + this.name + " 已经跑到 " + result + " 米 " + " 速度 " + speed + " 米/秒 ");
}
return result;
}
public void start() throws ExecutionException, InterruptedException {
// 线程池
ExecutorService executorService = Executors.newFixedThreadPool(3);
ThreadTest threadTest = new ThreadTest();
threadTest.name = "参赛者A";
ThreadTest threadTest1 = new ThreadTest();
threadTest1.name = "参赛者B";
ThreadTest threadTest2 = new ThreadTest();
threadTest2.name = "参赛者C";
Future<Integer> submit = executorService.submit(threadTest);
Future<Integer> submit1 = executorService.submit(threadTest1);
Future<Integer> submit2 = executorService.submit(threadTest2);
executorService.shutdown();
System.out.println(threadTest.name + "跑了" + submit.get() + "米");
System.out.println(threadTest1.name + "跑了" + submit1.get() + "米");
System.out.println(threadTest2.name + "跑了" + submit2.get() + "米");
}
}
}
package nuc.zm.MoreThread.create_thread;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* demo3
* 实现 callable 接口
* @author zm
* @date 2022/10/16
*/
public class Demo3 implements Callable<Double> {
/**
* 调用
* 抛 子类 异常
* @return {@link Double}
*/
@Override
public Double call() {
return (double) new Random().nextInt(10);
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
Demo3 demo3 = new Demo3();
FutureTask<Double> futureTask = new FutureTask<>(demo3);
Thread thread = new Thread(futureTask);
thread.start();
Object o = futureTask.get();
System.out.println(o);
}
}
现实案例 : 抛绣球案例 很多人争抢一个绣球(资源)
作用 : 利用一个特定的对象设置一个锁 , 在 多线程 并发访问的时候,同时只允许一个线程 获得这个锁,并执行特定代码。
执行后 会释放锁, 继续和其他线程争抢资源。
package nuc.zm.MoreThread;
/**
* 同步采样
* 演示 同步锁
* @author zm
* @date 2022/11/06
*/
public class SyncSample {
static class Printer {
public void print() {
try {
Thread.sleep(500);
System.out.print("你");
Thread.sleep(500);
System.out.print("好");
Thread.sleep(500);
System.out.print("世");
Thread.sleep(500);
System.out.print("界");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void start() {
Printer printer = new Printer();
for (int i = 0; i < 10; i++) {
PrintTask printTask = new PrintTask();
printTask.printer = printer;
Thread thread = new Thread(printTask);
thread.start();
}
}
static class PrintTask implements Runnable {
private Printer printer;
@Override
public void run() {
printer.print();
}
}
public static void main(String[] args) {
new SyncSample().start();
}
}
synchronized 代码块 – 任意对象
synchronized 方法 – this 当前对象
synchronized 静态方法 – 该类的字节码对象
package nuc.zm.MoreThread;
import java.util.concurrent.locks.Lock;
/**
* 同步采样
* 演示 同步锁
* @author zm
* @date 2022/11/06
*/
public class SyncSample {
static class Printer {
public void print() {
synchronized ("DADADADASDADAADADAD") {
try {
Thread.sleep(500);
System.out.print("你");
Thread.sleep(500);
System.out.print("好");
Thread.sleep(500);
System.out.print("世");
Thread.sleep(500);
System.out.print("界");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public synchronized void print1() {
try {
Thread.sleep(500);
System.out.print("你");
Thread.sleep(500);
System.out.print("好");
Thread.sleep(500);
System.out.print("世");
Thread.sleep(500);
System.out.print("界");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static synchronized void print2() {
try {
Thread.sleep(500);
System.out.print("你");
Thread.sleep(500);
System.out.print("好");
Thread.sleep(500);
System.out.print("世");
Thread.sleep(500);
System.out.print("界");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void start() {
Printer printer = new Printer();
for (int i = 0; i < 10; i++) {
PrintTask printTask = new PrintTask();
printTask.printer = printer;
Thread thread = new Thread(printTask);
thread.start();
}
}
static class PrintTask implements Runnable {
private Printer printer;
@Override
public void run() {
printer.print();
// printer.print1();
// Printer.print2();
}
}
public static void main(String[] args) {
new SyncSample().start();
}
}