* 多線程會搶佔cpu的資源,會佔用更多的服務器帶寬,這樣看起來就感覺會更快
創建線程有兩種方式:一種是創建Thread的子類,覆蓋Thread的run方法,還有一種是構造器傳入Runnable對象,使用Runnable對象的run方法:
public class TraditionlThread {
/**
* 創建線程的兩種方式
* @param args
*/
public static void main(String[] args) {
//方式一: 創建Thread的子類,覆蓋Thread的run方法
Thread thread = new Thread(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
};
thread.start();
// 源碼裏邊是Runnable對象不爲null就執行Runnable的run方法
// 方式二:構造函數中傳入Runnable對象
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}
}
});
thread2.start();
/*
* 兩種方式的區別:
* 第二種方式更加體現面向對象的方式
*
* */
// 同時有子類的run方法和Runnable對象,那麼程序會執行子類的run方法,子類的方法會覆蓋父類的
new Thread(new Runnable() {
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("runnable Thread:" + Thread.currentThread().getName());
}
}
}){
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("子類 Thread:" + Thread.currentThread().getName());
}
};
}.start();
}
}
線程的互斥:使用sychronized關鍵字
/**
* 傳統線程互斥技術
* 線程安全問題可以用銀行轉賬來解釋
* @author admin
*/
public class TraditionalSychronized{
public static void main(String[] args) {
OutputSafe2 out = new OutputSafe2();
write(out);
}
/**
* 這樣寫會出現錯亂
*/
static class Output{
public void output(String name){
for(int i=0; i<name.length(); i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
/**
* 方式一: 同步代碼塊
* 調用此方法的鎖要為同一個對象
*/
static class OutputSafe1{
public void output(String name){
synchronized(OutputSafe1.class){ // 鎖對象可以設置為當前類的字節碼對象
for(int i=0; i<name.length(); i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
}
/**
* 方式二:同步方法
* 方法裡邊的所有代碼都需要原子性 那麼用同步方法
*
* 注意:如果給同步方法中寫同步代碼塊很容易發生死鎖現象
*/
static class OutputSafe2{
public synchronized void output(String name){
for(int i=0; i<name.length(); i++){
System.out.print(name.charAt(i));
}
System.out.println();
}
}
public static void write(OutputSafe2 out){
new Thread(new Runnable() {
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
out.output("abcdef");
}
}
}).start();
new Thread(new Runnable() {
public void run() {
while(true){
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
out.output("uvwxyz");
}
}
}).start();
}
}
線程同步通信的例子:
main本身就是是一個線程
/**
* 題目:子線程循環10次,接着主線程循環5次,接着又回到子線程循環10次,主線程循環100次,
* 如此循環50次
*
*/
public class Test {
public static void main(String[] args) {
Business business = new Business();
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0; i<50; i++){
business.sub(i);
}
}
}).start();
for(int i= 0 ; i< 50 ; i++){
business.main(i);
}
}
static class Business{
boolean shouldSub = true;
public synchronized void sub(int i){
while(!shouldSub){ //使用while而不是if防止偽喚醒
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j=0; j<10; j++){
System.out.println("thread1 : " + j + "/" + i);
}
shouldSub = false;
this.notify();
}
public synchronized void main(int i){
while(shouldSub){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(int j=0; j<5; j++){
System.out.println("thread2 : " + j + "/"+ i);
}
shouldSub = true;
this.notify();
}
}
}