标签:voice pop 线程 dex 同步 方式 重写 lam throw
public class MyThread entends Thread {
@Override
public void run() {
// ...
}
}
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
}
public class MyRunnable implements Runnable {
@override
public void run() {
// ...
}
}
public static main(String[] args) {
MyRunnable mr = new MyRunnable();
new Thread(mr).start();
}
new Thread(()->{
// ...
}).start();
public class testStop implements Runnable {
// 标志位
private boolean flag = true;
@Override
public void run() {
while(flag) {
System.out.println("running ");
}
}
// 提供标示
public void stop() {
this.flag = false;
}
public static void main(String[] args) {
testStop ts = new testStop();
new Thread(ts).start();
// 主线程
for(int i = 1; i <= 50; i++) {
System.out.println("main " + i);
// 主线程i=30, 停止运行ts
if(i == 30) {
ts.stop();
System.out.println("stop!");
}
}
}
}
public class testSleep {
// sleep 存在异常InterruptedException
public static void countDown() throws InterruptedException {
int n = 10;
while(n > 0) {
Thread.sleep(100);
n--;
}
}
public static void main(String[] args) {
Date time = new Date(System.currentTimeMillis());
try {
System.out.println(new SimpleDateFormat("hh:mm:ss").format(System.currentTimeMillis()));
countDown();
time = new Date(System.currentTimeMillis());
System.out.println(new SimpleDateFormat("hh:mm:ss").format(System.currentTimeMillis()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class testYield {
public static void main(String[] args) {
MyYield my = new MyYield();
new Thread(my,"a").start();
new Thread(my, "b").start();
}
}
class MyYield implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " at work");
Thread.yield();
System.out.println(Thread.currentThread().getName() + " out of work");
}
}
public class testJoin {
public static void main(String[] args) throws InterruptedException {
MyJoin mj = new MyJoin();
Thread td = new Thread(mj);
td.start();
for(int i = 0; i < 10; i++) {
System.out.println("main: " + i);
if(i == 8) td.join();
}
}
}
class MyJoin implements Runnable {
@Override
public void run() {
for(int i = 1; i <= 100; i++) {
System.out.println("vip: " + i);
}
}
}
第一个线程访问,锁定同步监视器,执行其中的代码
第二个线程访问,发现同步监视器被锁定,无法访问
第一个线程访问完毕,解锁同步监视器
第二个线程访问,锁定访问
public class testPC {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Productor(container).start();
new Consumer(container).start();
}
}
class Productor extends Thread {
SynContainer container;
public Productor(SynContainer container) {
this.container = container;
}
public void run() {
for(int i = 0; i < 100; i++) {
container.push(new Chicken(i));
System.out.println("生产了" + i + "只鸡");
}
}
}
class Consumer extends Thread {
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了-->" + container.pop().id + "只鸡");
}
}
}
class Chicken {
int id;
public Chicken(int id) {
this.id = id;
}
}
class SynContainer {
Chicken[] chickens = new Chicken[10];
int count = 0;
public synchronized void push(Chicken chicken) {
if (count == chickens.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
chickens[count] = chicken;
count++;
this.notifyAll();
}
public synchronized Chicken pop() {
if(count == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Chicken chicken = chickens[count];
return chicken;
}
}
public class testPC2 {
public static void main(String[] args) {
Tv tv = new Tv();
new Player(tv).start();
new Watcher((tv)).start();
}
}
class Player extends Thread {
Tv tv;
public Player(Tv tv) {
this.tv = tv;
}
public void run() {
for (int i = 0; i < 20; i++) {
if(i % 2 == 0) {
this.tv.play("abc");
} else {
this.tv.play("def");
}
}
}
}
class Watcher extends Thread {
Tv tv;
public Watcher(Tv tv) {
this.tv = tv;
}
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
class Tv {
String voice;
// 标志位
boolean flag = true;
public synchronized void play(String voice) {
if(!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了:" + voice);
this.voice = voice;
this.flag = !this.flag;
}
public synchronized void watch() {
if(flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观看了:" + voice);
this.notifyAll();
this.flag = !this.flag;
}
}
public class testpool {
public static void main(String[] args) {
// 参数为线程池中线程个数
ExecutorService ser = Executors.newFixedThreadPool(10);
ser.execute(new MyThread());
ser.execute(new MyThread());
ser.execute(new MyThread());
ser.execute(new MyThread());
// 关闭
ser.shutdown();
}
}
class MyThread implements Runnable {
@Override
public void run() {
for(int i = 0; i < 4; i++) {
System.out.println(Thread.currentThread().getName());
}
}
}
标签:voice pop 线程 dex 同步 方式 重写 lam throw
原文地址:https://www.cnblogs.com/Hot-machine/p/13190348.html