java-day-24

java-day-24,第1张

java-day-24

2021.10.29 晴

Join:

package study;


public class Part01_Join {
	public static void main(String[] args) {
		Thread t1=new Thread(new Processer_01());
		Thread t2=new Thread(new Processer_01());
		t1.setName("t1");
		t2.setName("t2");
		t1.start();
		t2.start();
		try {
			// 执行到join的时候,因为是t1调用的,所以 main之后的代码,必须等t1执行完之后才能执行
			t1.join();

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		for (int i = 0; i < 5; i++) {
			System.out.println(Thread.currentThread().getName() + " : " + i);
		}
	}
}
class Processer_01 implements Runnable{

	@Override
	public void run() {
		for (int i = 0; i <5; i++) {
			System.out.println(Thread.currentThread().getName()+":"+i);
		}
	}
	
}

yield:

package study;

public class Part02_Yield {
	public static void main(String[] args) {
		Thread t1 = new Processor_02();
		t1.start();
		
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName()+" : "+i);
		}
	}
}
class Processor_02 extends Thread{
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			if (i % 2 == 0) {
				Thread.yield();
			}
			System.out.println(getName()+" : "+i);
		}
	}
}

线程同步:

package study;

public class Part03_synchronzied {
	public static void main(String[] args) {
		A a = new A(10);
		A a1 = new A(11);
		Thread t1 = new Thread(new Processor_03(a));
		Thread t2 = new Thread(new Processor_03(a));
		t1.setName("t1");
		t2.setName("t2");
		t1.start();
		t2.start();
	}
}

class Processor_03 implements Runnable {
	A a;

	public Processor_03(A a) {
		super();
		this.a = a;
	}

	@Override
	public void run() {
		a.m1();
	}
}

class A {
	int i;

	// 方法锁
//	public synchronized void m1() {
	public  void m1() {
		System.out.println("-----------");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// 代码块锁
		synchronized(this){
			i++;
			System.out.println(Thread.currentThread().getName() + " : " + i);
		}
		System.out.println("==========");

	}

	public A(int i) {
		super();
		this.i = i;
	}
}

Lock:

package study;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Part04_Lock {
	public static void main(String[] args) {

		Abc a = new Abc(10);
		Thread t1 = new Thread(new Processor_04(a));
		Thread t2 = new Thread(new Processor_04(a));
		t1.setName("t1");
		t2.setName("t2");
		t1.start();
		t2.start();

	}
}
class Processor_04 implements Runnable {
	Abc a;

	public Processor_04(Abc a) {
		super();
		this.a = a;
	}

	@Override
	public void run() {
		a.m1();
	}
}

class Abc {
	int i;
	// 创建锁对象
	Lock lock = new ReentrantLock();

	public void m1() {
		System.out.println("-----------");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// synchronized (this) {
		// 开始加锁
		lock.lock();
		i++;
		System.out.println(Thread.currentThread().getName() + " : " + i);
		// 解锁
		lock.unlock();
		// }

		System.out.println("==========");

	}

	public Abc(int i) {
		super();
		this.i = i;
	}
}

Timer:

package study;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class Part05_Timer {
	public static void main(String[] args) throws ParseException {
		// 创建定时器
				Timer timer = new Timer();
				// 1 做什么事
				// 2 开始时间 , 可以是时间(到了指定时间开始执行),也可以是毫秒数(当前时间开始,多长时间之后开始执行)
				// 3 执行的间隔时间
				// 两秒之后开始执行,并且每隔1秒执行一次
				// timer.schedule(new LogTimerTask(), 2000,1000);
				long m = System.currentTimeMillis();
				m += 1000 * 60;
				String string = "2021-10-29 10:50:00 000";
				Date d = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").parse(string);
				timer.schedule(new LogTimerTask(), d, 1000);
				System.out.println("----------");
	}
}
class LogTimerTask extends TimerTask {
	@Override
	public void run() {
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS")
				.format(new Date()));
	}
}

DeadLock:

package study;

public class Part06_DeadLock {
	public static void main(String[] args) {
		Object o1 = new Object();
		Object o2 = new Object();
		Thread t1 = new T1(o1,o2);
		Thread t2 = new Thread(new T2(o1,o2));
		t1.start();
		
		t2.start();
	}
}
class T1 extends Thread{
	Object o1;
	Object o2;
	
	public T1(Object o1, Object o2) {
		super();
		this.o1 = o1;
		this.o2 = o2;
	}
	@Override
	public void run() {
			synchronized (o1) {
				System.out.println("t1已进入o1 准备进入后o2");
				synchronized (o2) {
					System.out.println( "t1 执行完成");
				}
			}
	}
}
class T2 extends Thread{
	Object o1;
	Object o2;
	
	public T2(Object o1, Object o2) {
		super();
		this.o1 = o1;
		this.o2 = o2;
	}
	@Override
	public void run() {
			synchronized (o2) {
				System.out.println("t2已进入o2 准备进入后o1");
				synchronized (o1) {
					System.out.println( "t2 执行完成");
				}
			}
	}
}

wait:

package study;

import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Part07_wait {
	public static void main(String[] args) {
		Num num=new Num();
		Thread a1=new Thread(new A1(num));
		Thread a2=new Thread(new A2(num));
		a1.start();
		a2.start();
	}
}

class A1 implements Runnable {
	Num num;

	public A1(Num num){
		this.num=num;
	}
	
	@Override
	public void run() {
		while(true){
			num.m1();
		}		
	}

}

class A2 implements Runnable {
	Num num;

	public A2(Num num){
		this.num=num;
	}
	
	@Override
	public void run() {
		while(true){
	        num.m2();
		}
	}
}

class Num {
	int count = 0;

	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss  SSS");

	public synchronized void m1() {
		Date date=new Date();
		String canshu=sdf.format(date);
		System.out.println(canshu);
		System.out.println("a1:" + count);
		count++;
		try {
			// 唤醒所有在当前对象中睡眠的线程
			this.notifyAll();
			Thread.sleep(500);
			// 挂起 交出该对象持有的锁,让其他线程可以执行
			this.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public synchronized void m2() {
		Date date=new Date();
		String canshu=sdf.format(date);
		System.out.println(canshu);
		System.out.println("a2:" + count);
		count++;
		try {
			// 唤醒所有在当前对象中睡眠的线程
			this.notifyAll();
			Thread.sleep(500);
			// 挂起 交出该对象持有的锁,让其他线程可以执行
			this.wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4828929.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-10
下一篇 2022-11-10

发表评论

登录后才能评论

评论列表(0条)

保存