javaday24 多執行緒上

2021-07-09 02:09:46 字數 3878 閱讀 4567

2.多執行緒的應用場景

b:jvm的啟動是多執行緒的嗎

public class demo1_thread

for(int i = 0; i < 10000; i++)

}class demo

案例:public class demo2_thread

}class mythread extends thread

}public class demo3_thread

}class myrunnable implements runnable

}繼承thread

實現runnable介面

案例:package com.heima.thread;

public class demo4_thread

}}.start(); //4,開啟執行緒

//實現runnable介面

new thread(new runnable()

}}).start(); //4,開啟執行緒

}2.設定名字

public class demo1_name

public static void demo2()

}.start();

new thread()

}.start();

}public static void demo3()

};thread t2 = new thread()

};t1.setname("張三");

t2.setname("李四");

t1.start();

t2.start();

}public static void demo1()

}.start();

new thread("鳳姐")

}.start();

}public class demo2_currentthread

}.start();

new thread(new runnable()

}).start();

//設定主線程的名字

thread.currentthread().setname("我是主線程");

//直接寫在main方法中,獲取的是主線程

system.out.println(thread.currentthread().getname());

}案例:

public class demo3_sleep catch (interruptedexception e)

system.out.println(getname() + "...aaaaaaaaaa");}}

}.start();

new thread() catch (interruptedexception e)

system.out.println(getname() + "...bb");}}

}.start();

}public static void demo1() throws interruptedexception

}*thread類下的 setdaemon(boolean on)

, 設定乙個執行緒為守護執行緒, 該執行緒不會單獨執行, 當其他非守護執行緒都執行結束後, 自動退出

案例:public class demo4_daemon }};

thread t2 = new thread() }};

t2.setdaemon(true); //設定為守護執行緒

t1.start();

t2.start();

}public class demo5_join }};

thread t2 = new thread() catch (interruptedexception e)

}system.out.println(getname() + "...bb");}}

};t1.start();

t2.start();

}public class demo6_yield }

class mythread extends thread

system.out.println(getname() + "..." + i);

}}public class demo7_priority }};

thread t2 = new thread()}};

設定最大優先順序

t1.setpriority(thread.min_priority); //設定最小的執行緒優先順序

t2.setpriority(thread.max_priority); //設定最大的執行緒優先順序

t1.start();

t2.start();

}2.同步**塊

public class demo1_synchronized

}}.start();

new thread()

}}.start();

}class printer

}public void print2()

}} //定義的鎖類

class demo{}

public class demo2_synchronized

}}.start();

new thread()

}}.start();

}class printer2

public void print2(this)

}} 案例(靜態的同步方法):

public class demo2_synchronized

}}.start();

new thread()

}}.start();

}class printer2

public static void print2()

}public class demo3_ticket class ticket extends thread

system.out.println(getname() + "...這是第" + ticket-- + "號票");

}}}

案例(未加suo,出現賣負號票):

public class demo3_ticket class ticket extends thread

try catch (interruptedexception e)

system.out.println(getname() + "...這是第" + ticket-- + "號票");

}}}

案例(用同步實現):

public class demo3_ticket class ticket extends thread

try catch (interruptedexception e)

system.out.println(getname() + "...這是第" + ticket-- + "號票");}}

}案例:

public class demo4_ticket class myticket implements runnable

try catch (interruptedexception e)

system.out.println(thread.currentthread().getname() + "...這是第" + tickets-- + "號票");}}

}public class demo5_deadlock }}

}}.start();

new thread() }}

}}.start();

}

java day24 自定義列舉類

package com.itheima.domain public class testseason 自定義列舉類 class season 3.通過公共的方法來呼叫屬性 public string getseasonname public string getseasondesc 4.建立列舉類的...

java多執行緒(上)

b jvm的啟動是多執行緒的嗎 public class demo2 thread 1,定義類繼承thread class mythread extends thread public class demo3 runnable 1,自定義類實現runnable介面 class myrunnable ...

多執行緒爬蟲實現(上)

本文首發於知乎 爬蟲主要執行時間消耗是請求網頁時的io阻塞,所以開啟多執行緒,讓不同請求的等待同時進行,可以大大提高爬蟲執行效率。本文基於多執行緒 這裡開啟了10個執行緒 使用github的api,抓取fork cpython專案的所有5千多個專案資訊,將資料儲存到json檔案中。爬蟲所需技術 爬蟲...