Java多线程03:多线程深入+多线程综合实例
发布日期:2021-04-30 21:05:28 浏览次数:753 分类:精选文章

本文共 8366 字,大约阅读时间需要 27 分钟。

?????????

???????

???????????????????????Thread??start()???????????????????????Thread??stop()?????????????JDK1.2?????stop()?destroy()?suspend()?resume()????????????????????????????????????????

????????????????????????????????????????????

public class MyThread {
public static boolean flag = true;
public static void main(String[] args) throws InterruptedException {
new Thread(() -> {
int num = 0;
while (flag) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ", num=" + (num++));
}
}).start();
Thread.sleep(200);
flag = false; // ????
}
}

???????flag??????????????flag???false????????????????????????????????????????

??????

????????????????????????????????????????????????????????????????????????

Thread????setDaemon()?isDaemon()????????????????

public class MyThread {
public static void main(String[] args) throws InterruptedException {
Thread userThread = new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ", i=" + i);
}
}, "????");
Thread daemonThread = new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + ", i=" + i);
}
});
daemonThread.setDaemon(true);
daemonThread.start();
}
}

????????????????????????????????????????????????????GC?????????????????????????????

volatile???

????????volatile??????????????????????????????????????????????????????????volatile????????????????????????????????????????

???????volatile???????

public class MyThread implements Runnable {
private volatile int tickets = 5;
@Override
public void run() {
synchronized (this) {
while (this.tickets > 0) {
System.out.println(Thread.currentThread().getName() + "????, tickets=" + this.tickets--);
}
}
}
}
class ThreadDemo {
public static void main(String[] args) {
MyThread myThread = new MyThread();
new Thread(myThread, "???A").start();
new Thread(myThread, "???B").start();
new Thread(myThread, "???C").start();
}
}

???????tickets????volatile??????????????????????????????????????????????????????

?????????

????

????????????????????????????????????????????????????????0?-1?1?????

??????????

package com.lut.JavaPlus;
public class MyThread {
public static void main(String[] args) {
Resource resource = new Resource();
SubThread subThread = new SubThread(resource);
AddThread addThread = new AddThread(resource);
new Thread(addThread, "????A").start();
new Thread(addThread, "????B").start();
new Thread(subThread, "????A").start();
new Thread(subThread, "????B").start();
}
}
class AddThread implements Runnable {
private Resource resource;
public AddThread(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
this.resource.add();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class SubThread implements Runnable {
private Resource resource;
public SubThread(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
this.resource.sub();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class Resource {
private int num = 0;
private boolean flag = true;
public synchronized void add() throws Exception {
if (this.flag == false) {
super.wait();
}
Thread.sleep(100);
this.num++;
System.out.println("?????-" + Thread.currentThread().getName() + "? num=" + this.num);
this.flag = false;
super.notifyAll();
}
public synchronized void sub() throws Exception {
if (this.flag == true) {
super.wait();
}
Thread.sleep(200);
this.num--;
System.out.println("?????-" + Thread.currentThread().getName() + "? num=" + this.num);
this.flag = true;
super.notifyAll();
}
}

??????add()?sub()????flag??????????????????????????????

????

?????????????????????????????????????????????????

??????????

package com.lut.JavaPlus;
public class MyThread {
public static void main(String[] args) {
Resource resource = new Resource();
new Thread(new Producer(resource)).start();
new Thread(new Consumer(resource)).start();
}
}
class Producer implements Runnable {
private Resource resource;
public Producer(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
this.resource.make();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable {
private Resource resource;
public Consumer(Resource resource) {
this.resource = resource;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
this.resource.get();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class Computer {
private static int count = 0;
private String name;
private double price;
public Computer(String name, double price) {
this.name = name;
this.price = price;
count++;
}
@Override
public String toString() {
return "?count=" + count + "-Computer{" +
"name='" + name + '\'' +
", price=" + price +
"}?";
}
}
class Resource {
private Computer computer;
public synchronized void make() throws Exception {
if (this.computer != null) {
super.wait();
}
this.computer = new Computer("DELL", 3000);
System.out.println("????:" + computer.toString());
super.notifyAll();
}
public synchronized void get() throws Exception {
if (this.computer == null) {
super.wait();
}
System.out.println("????:" + computer.toString());
this.computer = null;
super.notifyAll();
}
}

??????make()?get()????flag???????????????????????make()??????????get()?????????????

????

????????????????????????????????????????????????????????????????????????Callable?FutureTask????

??????????

public class MyThread {
public static void main(String[] args) throws Exception {
ThreadDemo threadDemo = new ThreadDemo();
FutureTask
taskA = new FutureTask<>(threadDemo);
FutureTask
taskB = new FutureTask<>(threadDemo);
FutureTask
taskC = new FutureTask<>(threadDemo);
new Thread(taskA, "???A").start();
new Thread(taskB, "???B").start();
new Thread(taskC, "???C").start();
System.out.println(taskA.get());
System.out.println(taskB.get());
System.out.println(taskC.get());
}
}
class ThreadDemo implements Callable
{
private boolean flag = false;
@Override
public String call() throws Exception {
synchronized (this) {
if (this.flag == false) {
this.flag = true;
return Thread.currentThread().getName() + "?????";
} else {
return Thread.currentThread().getName() + "?????";
}
}
}
}

??????Callable???????call()???????String???FutureTask??? Callable?????????get()?????????????????????????????????????????

上一篇:厚积薄发打卡Day33 :[kuangStudy] GoF23通俗易懂的设计模式之 <适配器模式>
下一篇:JavaBase-IO流-随机存取文件流

发表评论

最新留言

表示我来过!
[***.240.166.169]2026年06月06日 06时20分22秒