1.線程創(chuàng)建
- 繼承Thread
- 實現(xiàn)Runnable接口
- 實現(xiàn)callable接口
1.1繼承Thread
* 線程創(chuàng)建菊值,
* 繼承Thread類拧烦,重寫run方法
*/
public class ThreadDemo1 extends Thread{
private String name;
public ThreadDemo1(String name){
this.name = name;
}
@Override
public void run() {
System.out.println("hello "+name);
}
}
說下靜態(tài)代理:
靜態(tài)代理相關(guān):
什么是靜態(tài)代理阔蛉,
對原有對象的不滿意隙袁,想增強(qiáng)功能有三種方式
- 繼承:繼承原有類削咆,加上更多功能得到新的類蚪拦,但是如果后來需要很多功能杖剪,會繼承很多類,層級過高
- 包裝: 原理也是代理
- 代理:新建一個代理類驰贷,實現(xiàn)原有接口盛嘿,對對象進(jìn)行增強(qiáng)
靜態(tài)代理:
需要有原有對象,
代理對象
公共接口
public class StaticProxy {
public static void main(String[] args) {
Man man = new Man("張三");
// man.coding();
ProxyMan proxyMan = new ProxyMan(man);
proxyMan.coding();
}
}
/**
* 程序員接口括袒,敲代碼的方法
*/
interface Programer{
void coding();
}
class Man implements Programer{
public String name;
public String getName() {
return name;
}
public Man(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void coding() {
System.out.println(name+"正在辛苦的敲代碼");
}
}
class ProxyMan implements Programer{
public Man man;
public ProxyMan(Man man) {
this.man = man;
}
@Override
public void coding() {
startComputer();
System.out.println(man.getName()+"正在辛苦的敲代碼");
closeComputer();
}
private void closeComputer() {
System.out.println(man.getName()+"關(guān)閉了電腦");
}
private void startComputer() {
System.out.println(man.getName()+"開啟電腦");
}
}
1.2實現(xiàn)Runnable接口
public class ThreadDemo2 {
public static void main(String[] args) {
Thread one = new Thread(new Demo2("線程1"));
one.start();
for (int j=0;j<100;j++){
System.out.println("zhangsan--"+j);
}
}
}
class Demo2 implements Runnable{
public String name;
public Demo2(String name) {
this.name = name;
}
@Override
public void run() {
for (int i =0;i<100;i++){
System.out.println(name+"--"+i);
}
}
}
Thread實現(xiàn)了Runnable接口次兆,實際上是對線程的代理
1.3 實現(xiàn)Callable接口
2.線程狀態(tài)
它要經(jīng)過新建(New)、就緒(Runnable)锹锰、運行(Running)芥炭、阻塞(Blocked)和死亡(Dead)五種狀態(tài)
2.1 線程阻塞方法
- join
- sleep
- yield
Join方式(可用于多個線程順序執(zhí)行)
public class JoinTreadDemo1 extends Thread{
public static void main(String[] args) throws InterruptedException {
JoinTreadDemo1 demo = new JoinTreadDemo1();
demo.start();
for (int j=0;j<100;j++){
if (j==50){
demo.join();//main線程被阻塞,當(dāng)demo這個線程結(jié)束之后才會執(zhí)行main
}
System.out.println("main--"+j);
}
}
@Override
public void run() {
for (int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+i);
}
}
}
yield方式恃慧,用于暫停自己
public class YeildThreadDemo extends Thread{
public static void main(String[] args) {
YeildThreadDemo yeildThreadDemo = new YeildThreadDemo();
yeildThreadDemo.start();
for (int j=0;j<100;j++){
if (j==50){
Thread.yield();
}
System.out.println("main--"+j);
}
}
@Override
public void run() {
for (int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+i);
}
}
}
sleep方式园蝠,常用于模擬網(wǎng)絡(luò)延遲,或者倒計時
3. 鎖相關(guān)
3.1 模擬12306買票
10張票痢士,由三個窗口售賣彪薛,如果不加鎖,多線程情況下會出現(xiàn)假數(shù)據(jù)良瞧,賣相同的票
public class Web12306 implements Runnable {
private int num=50;//50張票
@Override
public void run() {
while (true){
if (num<0){
break;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"售出了第"+num+"張票");
num--;
}
}
public static void main(String[] args) {
Web12306 web12306 = new Web12306();
//代理類
Thread demo1 = new Thread(web12306, "黃牛1");
Thread demo2 = new Thread(web12306, "黃牛2");
Thread demo3 = new Thread(web12306, "黃牛3");
demo1.start();
demo2.start();
demo3.start();
}
}
加鎖實現(xiàn)
鎖只能鎖引用類型
- 鎖方法
- 鎖代碼塊
- 鎖字節(jié)碼文件
public class SynThreadDemo1 {
public static void main(String[] args) {
webtest web12306 = new webtest();
//代理類
Thread demo1 = new Thread(web12306, "黃牛1");
Thread demo2 = new Thread(web12306, "黃牛2");
Thread demo3 = new Thread(web12306, "黃牛3");
demo1.start();
demo2.start();
demo3.start();
}
}
class webtest implements Runnable{
private int num=10;//50張票
public boolean flag=true;
@Override
public void run() {
while (flag){
test3();
}
}
public void test3() {//鎖代碼塊
synchronized (this) {
if (num <= 0) {
flag = false;
return;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "售出了第" + num-- + "張票");
}
}
public synchronized void test2() { //方法鎖
if (num<=0){
flag = false;
return;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"售出了第"+num--+"張票");
}
public void test1() { //不安全
if (num<=0){
flag = false;
return;
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"售出了第"+num+"張票");
num--;
}
}
3.2 單例模式的實現(xiàn)
單例模式
- 餓漢模式
- 懶漢模式
創(chuàng)建一個靜態(tài)對象陪汽,調(diào)用靜態(tài)方法,實現(xiàn)懶加載 - 雙重檢查
- 靜態(tài)內(nèi)部類
public class MyJvm {
public static void main(String[] args) {
Jvm1 jvm1 = new Jvm1();
Jvm1 jvm2 = new Jvm1();
System.out.println(jvm1);
System.out.println(jvm2);
}
}
/**
* 餓漢模式
* 當(dāng)類初始化時候就會創(chuàng)建實例
*/
class Jvm1{
private static Jvm1 jvm1 = new Jvm1();
public static Jvm1 getInstance(){
return jvm1;
}
}
/**
* 懶漢式褥蚯,當(dāng)調(diào)用方法時候才會初始化挚冤,懶加載
*/
class Jvm2{
private static Jvm2 jvm2 = null;
public static Jvm2 getInstance(){
if (jvm2 == null) {
jvm2 = new Jvm2();
}
return jvm2;
}
}
/**
* 雙重檢查
*
*/
class Jvm3{
private static Jvm3 jvm3 = null;
public static Jvm3 getInstance(){
if (jvm3 ==null){
synchronized (Jvm3.class) {
if (jvm3 == null) {
jvm3 = new Jvm3();
}
}
}
return jvm3;
}
}
/**
* 靜態(tài)內(nèi)部類
*/
class Jvm4{
private static class get{
private static Jvm3 jvm3 = new Jvm3();
}
public static Jvm3 getInstance(){
return get.jvm3;
}
}
4.Timer
定時器
定時做什么任務(wù)
倒計時案例
public class TimerDemo {
public static void main(String[] args) throws ParseException {
String s ="02:00:00";
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date parse = format.parse(s);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int i=1;
@Override
public void run() {
System.out.println("so easy..."+format.format(parse.getTime()-1000*i++));
}
},1000,1000);
}
}
更多交流 qq群:552113611
碼云源碼:https://gitee.com/zhangqiye/Thread