Java多線程基礎代碼

package myThread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* 也是一種創建線程的方式 需要研究
* @author Administrator
*
*/
public class callaableFutures {

public static void main(String[] args) throws InterruptedException,
        ExecutionException {
    ExecutorService ex = Executors.newFixedThreadPool(2);
    TestThread t = new TestThread();
    Future f = ex.submit(t);
    Object a = f.get();
    System.out.println(a);
    ex.shutdown();
}

}

class TestThread implements Callable {

@Override
public Object call() throws Exception {
    int a = 0;
    for (int i = 0; i < 1000; i++) {
        a += i;
    }
    return a;
}

}

package myThread;

public class JoinTest {

public static void main(String[] args) throws InterruptedException {
    Thread t = new Thread( new JoinTests());
    // t.start()貌似必須放在main執行的方法體之前

    // 要不然那代碼執行完了這個線程才就緒
    t.start();
    for (int i = 1; i < 500; i++) {

        if (i == 100) {
            // 誰調用join 誰得以執行
            t.join();
        }

        System.out.println("main---" + i);
    }

}

}

class JoinTests implements Runnable {
@Override
public void run() {
for (int i = 1; i < 500; i++) {
System.out.println(“run方法—–” + i);
}

}

}

package myThread;

/**
* 生產者消費者模型 Java 多線程基礎就是這些
*
* @author Administrator
*
*/
public class ProducerUser {

public static void main(String[] args) {
    They they = new They();
    Producer p = new Producer(they);
    User u = new User(they);
    Thread t1 = new Thread(p);
    Thread t2 = new Thread(u);
    t1.start();
    t2.start();
}

}

/**
* 生產者
*
* @author Administrator
*
*/
class Producer implements Runnable {
private They they;
private boolean flag;

public Producer(They they) {
    this.they = they;
    this.flag = they.flag;
}

public boolean isFlag() {
    return flag;
}

public void setFlag(boolean flag) {
    this.flag = flag;
}

@Override
public void run() {
    for (int i = 0; i < 30; i++) {

        try {
            // 指的是當前線程
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("生產者生產==" + i);
    }
}

}

/**
* 消費者
*
* @author Administrator
*
*/
class User implements Runnable {
private They they;
private boolean flag;

public User(They they) {
    this.they = they;
    this.flag = they.flag;
}

public boolean isFlag() {
    return flag;
}

public void setFlag(boolean flag) {
    this.flag = flag;
}

@Override
public void run() {
    for (int i = 0; i < 30; i++) {

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("消費者消費==" + i);
    }
}

}

class They {
boolean flag = false;

public synchronized void hhh() throws InterruptedException {
    while (flag) {
        this.wait();
    }
    Thread.sleep(500);
    flag = false;
    this.notifyAll();

}

}

package myThread;

import java.util.Timer;
import java.util.TimerTask;

/**
* 任務調度
*
* @author Administrator
*
*/
public class TimerTest {
// 跟線程差不多 要寫到main方法裏面
public static void main(String[] args) {
Timer time = new Timer();
// 方法咋寫的 看下api
// 遇到問題要常看api 常看源碼和源碼的註釋
time.schedule(new TimerTask() {

        @Override
        public void run() {
            System.out.println("定時任務");

        }
    }, 1000, 200);
}

}

package myThread;
//sleep 當前線程休眠 但不會釋放鎖
public class YieldTest {
public static void main(String[] args) {
Runner1 r = new Runner1();
Thread t = new Thread(r);
t.start();
for (int i = 0; i < 500; i++) {
if (i % 10 == 0) {
// t.yeild()
// 誰調用誰暫停 但是不一定 cpu讓哪個線程執行沒法控制
// Thread.yeild() 寫在哪個線程的方法體內就指的哪個線程
Thread.yield();
}
System.out.println(“main—” + i);
}
}
}

class Runner1 implements Runnable {

@Override
public void run() {
    for (int i = 0; i < 500; i++) {
        System.out.println("run---" + i);
    }

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章