版權聲明:本文為部落客原創文章,轉載請注明出處。 https://blog.csdn.net/twilight_karl/article/details/53487378
Callable 可以傳回值
ExecutorServers
ExecutorServers ser = ExevuteServers.newFixedThread()
Future result = sesr.submit(類);
ser.shutdownNow();
停止線程
線程類中定義線程體使用的标志
線程提内使用該标志
提供對外的方法改變該辨別
外部調用方法改變辨別
run (){
while(flag){
}
}
阻塞
join()合并線程
yeild()暫停目前正在執行的線程,執行其他線程static
sleep()暫停,不釋放鎖
–> 與時間相關
–> 模拟網絡延時
system.currentTimeMillis()目前時間
//join 合并線程,執行目前線程時加入另一個線程,并等待另一個線程執行完畢
import java.util.Locale.Category;
public class Test1 {
public static void main(String [] args) throws InterruptedException{
home m = new home();
Thread cat = new Thread(m);
cat.setName("cat");
cat.start();
for(int i = 0 ; i< 10; i++){
cat.join();
System.out.println("main");
}
}
}
class home implements Runnable{
public void run() {
for (int i = 0 ; i < 10 ; i++){
System.out.println("This is "+Thread.currentThread().getName()+i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// yield 暫停本線程執行其他線程(雖然在我電腦上試沒有任何反應。。。)
for(int i = 0 ; i< 10; i++){
// if(i%2==0)
Thread.yield();
System.out.println("main");
}
優先級
*MAX_PRIOIRTY 10
NORM_PRIOIRTY 5
MIN_PRIOIRTY 1*
isAlive() 判斷線程是否還活着
Thread.currentThread()目前線程 static
getName()
*setPriority()
優先級代表機率,不是絕對的先後順序*
線程的同步與鎖定
多個線程通路同一資源:線程安全
StringBuffer–append
hashtable
1.同步塊/同步方法
同步方法:
package 線程;
import java.util.*;
import One.sss;
public class tongbu {
public static void main(String [] args) throws InterruptedException{
a m = new a();
Thread cat = new Thread(m);
Thread dog = new Thread(m);
cat.setName("cat");
dog.setName("dog");
cat.start();
dog.start();
}
}
class a implements Runnable{
int s =10;
@Override
public void run() {
// TODO Auto-generated method stub
test1();
}
private synchronized void test1(){
for (int i = 0 ; i < 10 ; i++){
if (s<=0)
break;
System.out.println(Thread.currentThread().getName()+"get"+s--);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
同步塊:
synchronize(引用類型)
{ 同步的部分 }
//同步塊,參數為引用`
private void test2(){
synchronized(this){
for (int i = 0 ; i < 10 ; i++){
if (s<=0)
break;
System.out.println(Thread.currentThread().getName()+"get"+s--);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
鎖定範圍難把握
線程安全的效率相對低下
單例設計模式
一個類隻有一個對象
雙重檢查
1、懶漢式:
将構造器設為私有。避免外部直接建立對象
聲明私有的靜态屬性
對外部提供通路屬性的靜态方法,確定對象的存在
鎖定靜态的資訊(XXX.class)
2、餓漢式
聲明私有的靜态屬性,同時建立對象
對外部提供通路屬性的靜态方法
RunTime
//懶漢式
//可以滿足要求但是效率不高
package 線程;
public class 單例 {
public static void main(String[] args) throws InterruptedException {
test s1 = new test();
test s2 = new test();
s1.start();
s2.start();
}
}
class Single {
private static Single data = null;
// 私有的構造函數
private Single(){
}
//提供外界通路的函數
public static synchronized Single getInstance() throws InterruptedException{
if(null == data){
Thread.sleep(1000);
data = new Single();
}
return data;
}
}
class test extends Thread{
public void run(){
try {
System.out.println(Single.getInstance());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(b);
}
}
//可以使用塊同步
public static Single getInstance2() throws InterruptedException{
synchronized(Single.class){
if(null == data){
Thread.sleep(1000);
data = new Single();
}
return data;
}
}
//雙重檢查
public static Single getInstance3() throws InterruptedException {
if (null == data) {
synchronized (Single.class) {
if (null == data) {
Thread.sleep(1000);
data = new Single();
}
}
}
return data;
}
餓漢式
private static Single data = new Single();
聲明時建立
過多的同步方法可能造成死鎖
生産者消費者模式
wait()等待,釋放鎖
notify() 通知正在等待的線程-和同步一起使用
notifyAll() 通知多個線程
package 生産者消費者模式;
import javax.security.auth.login.FailedLoginException;
public class Moive {
private String moive;
boolean flag = true;
public synchronized void play(String m){
if (!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (flag){
try {
this.moive = m;
Thread.sleep(500);
System.out.println("已經放映電影:"+moive);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = false;
notify();
}
}
public synchronized void watch() {
if (flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (!flag) {
try {
Thread.sleep(200);
System.out.println("正在觀看電影:" + moive);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
flag = true;
notify();
}
}
}
Timer定時器
*一種工具,線程用其安排以後在背景線程中執行的任務。可安排任務執行一次,或者定期重複執行。
*
schedule(TimerTask task, Date firstTime, long period)
安排指定的任務在指定的時間開始進行重複的固定延遲執行。
schedule(TimerTask task, Date time)
安排在指定的時間執行指定的任務。
schedule(TimerTask task, long delay)
安排在指定延遲後執行指定的任務。
::TimerTask(使用Runnable接口的方法)