发布于2020-11-19 20:41 阅读(1568) 评论(0) 点赞(27) 收藏(2)
package com.csdn.day022;
public class Demo02_SynchronizedMethod {
public static void main(String[] args) {
PrintString ps = new PrintString();
new Thread() {
@Override
public void run() {
while(true) {
//ps.test1();
PrintString.test1();
}
}
}.start();
new Thread() {
@Override
public void run() {
while(true) {
//ps.test2();
PrintString.test2();
}
}
}.start();
}
}
/*
* 非静态同步方法:同步锁对象是this
* 静态同步方法:同步所对象是类的字节码对象
*
*
* */
class PrintString {
/*public synchronized void test1() {
System.out.print("松");
System.out.print("活");
System.out.print("弹");
System.out.println("抖");
}
public synchronized void test2() {
System.out.print("闪");
System.out.print("电");
System.out.println("鞭");
}*/
/*public void test1() {
synchronized (this) {
System.out.print("松");
System.out.print("活");
System.out.print("弹");
System.out.println("抖");
}
}
public void test2() {
synchronized (this) {
System.out.print("闪");
System.out.print("电");
System.out.println("鞭");
}
}*/
/*public static synchronized void test1() {
System.out.print("松");
System.out.print("活");
System.out.print("弹");
System.out.println("抖");
}
public static synchronized void test2() {
System.out.print("闪");
System.out.print("电");
System.out.println("鞭");
}*/
public static void test1() {
synchronized (PrintString.class) {
System.out.print("松");
System.out.print("活");
System.out.print("弹");
System.out.println("抖");
}
}
public static void test2() {
synchronized (PrintString.class) {
System.out.print("闪");
System.out.print("电");
System.out.println("鞭");
}
}
}
package com.csdn.day022;
public class Demo03_DeadLock {
public static void main(String[] args) {
Thread t1 = new Thread("家") {
@Override
public void run() {
synchronized ("车钥匙") {
System.out.println("车钥匙在家里");
synchronized ("家钥匙") {
System.out.println("进去家得到车钥匙,既能进去家,也能进去车");
}
}
}
};
Thread t2 = new Thread("车") {
@Override
public void run() {
synchronized ("家钥匙") {
System.out.println("家钥匙在车里");
synchronized ("车钥匙") {
System.out.println("进去车得到家钥匙,既能进去车,也能进去家");
}
}
}
};
t1.start();
t2.start();
}
}
三个窗口,同时售卖100张火车票
打印某个窗口卖出了1张票,还剩几张
package com.csdn.day022;
/**
* 三个窗口,同时售卖100张火车票 打印某个窗口卖出了1张票,还剩几张
*
* 三个窗口:三条线程
*
* @author Zihuatanejo
*
*/
public class Demo04_Exercise {
public static void main(String[] args) {
Ticket t = new Ticket();
Thread t1 = new Thread(t, "窗口1");
Thread t2 = new Thread(t, "窗口2");
Thread t3 = new Thread(t, "窗口3");
t1.start();
t2.start();
t3.start();
}
public static void test1() {
Window w1 = new Window("窗口1");
Window w2 = new Window("窗口2");
Window w3 = new Window("窗口3");
w1.start();
w2.start();
w3.start();
}
}
class Ticket implements Runnable {
private int tickets = 100;
@Override
public void run() {
while(true) {
synchronized (this) {
if (tickets == 0) {
break;
}
tickets--;
System.out.println(Thread.currentThread().getName() + "卖出了1张票,还剩" + tickets + "张");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Window extends Thread {
private static int tickets = 100;
public Window() {
super();
}
public Window(Runnable target, String name) {
super(target, name);
}
public Window(Runnable target) {
super(target);
}
public Window(String name) {
super(name);
}
@Override
public void run() {
while (true) {
synchronized (Window.class) {
if (tickets == 0) {
break;
}
tickets--;
System.out.println(getName() + "卖出了1张票,还剩" + tickets + "张");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.csdn.day022;
public class Demo05_OtherMethod {
public static void main(String[] args) {
Thread t1 = new Thread() {
@Override
public void run() {
for (int i = 1; i <= 1000; i++) {
System.out.println(i);
}
}
};
Thread t2 = new Thread("=====线程1111") {
@Override
public void run() {
Thread.yield();
for (int i = 9000; i <= 9999; i++) {
System.out.println(i + getName());
}
}
};
t2.start();
t1.start();
}
public static void test1() {
Thread t1 = new Thread() {
@Override
public void run() {
for (int i = 1; i <= 1000; i++) {
System.out.println(i);
}
}
};
Thread t2 = new Thread("=====线程1111") {
@Override
public void run() {
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 9000; i <= 9999; i++) {
System.out.println(i + getName());
}
}
};
t1.start();
t2.start();
}
}
package com.csdn.day022;
public class Demo06 {
public static void main(String[] args) throws InterruptedException {
//13579
//248 10
MyTask mt = new MyTask();
Thread t1 = new Thread(mt, "------线程1111");
Thread t2 = new Thread(mt, "==线程2222");
t1.start();
t2.start();
Thread.sleep(100);
}
}
class MyTask implements Runnable {
private int num = 1;
@Override
public void run() {
while (true) {
synchronized (this) {
this.notify();
if (num > 100) {
break;
}
System.out.println(num + Thread.currentThread().getName());
num++;
try {
Thread.sleep(30);//让线程休眠,但是不释放同步锁对象
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try {
this.wait();//让线程进入无限期等待并且释放同步锁对象
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package com.csdn.day022;
public class Demo07_SingletonHungry {
public static void main(String[] args) {
SingletonHungry sh1 = SingletonHungry.getInstance();
SingletonHungry sh2 = SingletonHungry.getInstance();
System.out.println(sh1);
System.out.println(sh2);
}
}
class SingletonHungry {
//1.构造方法私有化:限制外界创建对象
private SingletonHungry() {}
//2.在类中创建好对象
private static SingletonHungry sh = new SingletonHungry();
//3.在类中对外提供获取对象的方式
public static SingletonHungry getInstance() {
return sh;
}
}
懒汉式:在加载类的时候不着急创建对象,等到调用方法经过好几层判断后,非得创建不可才去创建,就像懒汉,能不做就不做,能拖就拖
package com.csdn.day022;
public class Demo08_SingletonLazy {
public static void main(String[] args) {
}
}
class SingletonLazy {
//1.构造方法私有化限制外界创建对象
private SingletonLazy() {}
//2.在类中提前创建好对象
private static SingletonLazy sl;
//A B
//3.对外提供公开的获取方式
public static SingletonLazy getInstance() {
//内外层的判断一个都不能少,外层主要提高效率,但是如果将内层if去掉,会重新出现线程安全问题
//3.多线程环境下如果每一次都获取同步锁对象再去判断效率低下,外层加上一个判断,能尽可能的提高效率
if (sl == null) {
//2.多线程环境下,无法保证1的线程安全,所以加上同步代码块保证操作的完整性
synchronized (SingletonLazy.class) {
//1.判断当前对象是否存在,如果存在就不创建不存在才创建
if (sl == null) {
sl = new SingletonLazy();//12345
}
}
}
return sl;
}
}
package com.csdn.day022;
public class Demo09_SingletonOld {
public static void main(String[] args) {
SingletonOld so1 = SingletonOld.so;
SingletonOld so2 = SingletonOld.so;
System.out.println(so1 == so2);
System.out.println(so1);
System.out.println(so2);
}
}
class SingletonOld {
//构造方法私有化
private SingletonOld() {}
//提前在类中创建好对象
public static final SingletonOld so = new SingletonOld();
}
package com.csdn.day022;
public class Demo10_GetState {
public static void main(String[] args) {
Thread t = new Thread() {
@Override
public void run() {
for (int i = 0; i <= 10; i++) {
}
}
};
t.start();
while (true) {
System.out.println(t.getState());
}
}
public static void test4() {
Thread t = new Thread() {
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
System.out.println(i);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
while (true) {
System.out.println(t.getState());
}
}
public static void test3() {
Thread t1 = new Thread("家") {
@Override
public void run() {
synchronized ("车钥匙") {
System.out.println("车钥匙在家里");
synchronized ("家钥匙") {
System.out.println("进去家得到车钥匙,既能进去家,也能进去车");
}
}
}
};
Thread t2 = new Thread("车") {
@Override
public void run() {
synchronized ("家钥匙") {
System.out.println("家钥匙在车里");
synchronized ("车钥匙") {
System.out.println("进去车得到家钥匙,既能进去车,也能进去家");
}
}
}
};
t1.start();
t2.start();
while (true) {
System.out.println(t1.getState());
System.out.println(t2.getState());
}
}
public static void test2() {
Thread t = new Thread() {
@Override
public void run() {
while(true) {
}
}
};
t.start();
System.out.println(t.getState());
}
public static void test1() {
Thread t = new Thread();
System.out.println(t.getState());
}
}
package com.csdn.day022;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Demo11_Executors {
public static void main(String[] args) {
//1.获取线程池
ExecutorService es = Executors.newFixedThreadPool(2);
//2.创建任务对象
Runnable task1 = new Runnable() {
public void run() {
for (int i = 0; i < 1000; i++) {
System.out.println(i + "-----" + Thread.currentThread().getName());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Runnable task2 = new Runnable() {
public void run() {
for (int i = 1000; i < 2000; i++) {
System.out.println(i + "-----" + Thread.currentThread().getName());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Runnable task3 = new Runnable() {
public void run() {
for (int i = 9000; i < 10000; i++) {
System.out.println(i + "-----" + Thread.currentThread().getName());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
//3.将任务提交到线程池
es.submit(task1);
es.submit(task2);
es.submit(task3);
//es.shutdown();
es.shutdownNow();
}
}
原文链接:https://blog.csdn.net/xxxx1982/article/details/109755851
作者:java之恋
链接:http://www.javaheidong.com/blog/article/944/5d3206670339cb560569/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!