java必學必會之線程(2)。本站提示廣大學習愛好者:(java必學必會之線程(2))文章只能為提供參考,不一定能成為您想要的結果。以下是java必學必會之線程(2)正文
1、線程的優先級別
線程優先級其余應用典范:
package cn.galc.test;
public class TestThread6 {
public static void main(String args[]) {
MyThread4 t4 = new MyThread4();
MyThread5 t5 = new MyThread5();
Thread t1 = new Thread(t4);
Thread t2 = new Thread(t5);
t1.setPriority(Thread.NORM_PRIORITY + 3);// 應用setPriority()辦法設置線程的優先級別,這裡把t1線程的優先級別停止設置
/*
* 把線程t1的優先級(priority)在正常優先級(NORM_PRIORITY)的基本上再進步3級
* 如許t1的履行一次的時光就會比t2的多許多
* 默許情形下NORM_PRIORITY的值為5
*/
t1.start();
t2.start();
System.out.println("t1線程的優先級是:" + t1.getPriority());
// 應用getPriority()辦法獲得線程的優先級別,打印出t1的優先級別為8
}
}
class MyThread4 implements Runnable {
public void run() {
for (int i = 0; i <= 1000; i++) {
System.out.println("T1:" + i);
}
}
}
class MyThread5 implements Runnable {
public void run() {
for (int i = 0; i <= 1000; i++) {
System.out.println("===============T2:" + i);
}
}
}
run()辦法一停止,線程也就停止了。
2、線程同步
synchronized症結字的應用典范:
package cn.galc.test;
public class TestSync implements Runnable {
Timer timer = new Timer();
public static void main(String args[]) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");// 設置t1線程的名字
t2.setName("t2");// 設置t2線程的名字
t1.start();
t2.start();
}
public void run() {
timer.add(Thread.currentThread().getName());
}
}
class Timer {
private static int num = 0;
public/* synchronized */void add(String name) {// 在聲明辦法時參加synchronized時表現在履行這個辦法的進程當中以後對象被鎖定
synchronized (this) {
/*
* 應用synchronized(this)來鎖定以後對象,如許就不會再湧現兩個分歧的線程同時拜訪統一個對象資本的成績了 只要當一個線程拜訪停止後才會輪到下一個線程來拜訪
*/
num++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + ":你是第" + num + "個應用timer的線程");
}
}
}
線程逝世鎖的成績:
package cn.galc.test;
/*這個小法式模仿的是線程逝世鎖的成績*/
public class TestDeadLock implements Runnable {
public int flag = 1;
static Object o1 = new Object(), o2 = new Object();
public void run() {
System.out.println(Thread.currentThread().getName() + "的flag=" + flag);
/*
* 運轉法式後發明法式履行到這裡打印出flag今後就不再往下履行前面的if語句了
* 法式也就逝世在了這裡,既不往下履行也不加入
*/
/* 這是flag=1這個線程 */
if (flag == 1) {
synchronized (o1) {
/* 應用synchronized症結字把對象01鎖定了 */
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2) {
/*
* 後面曾經鎖住了對象o1,只需再能鎖住o2,那末就可以履行打印出1的操作了
* 可是這裡沒法鎖定對象o2,由於在別的一個flag=0這個線程外面曾經把對象o1給鎖住了
* 雖然鎖住o2這個對象的線程會每隔500毫秒睡眠一次,可是在睡眠的時刻依然是鎖住o2不放的
*/
System.out.println("1");
}
}
}
/*
* 這裡的兩個if語句都將沒法履行,由於曾經形成了線程逝世鎖的成績
* flag=1這個線程在期待flag=0這個線程把對象o2的鎖解開,
* 而flag=0這個線程也在期待flag=1這個線程把對象o1的鎖解開
* 但是這兩個線程都不肯意解開鎖住的對象,所以就形成了線程逝世鎖的成績
*/
/* 這是flag=0這個線程 */
if (flag == 0) {
synchronized (o2) {
/* 這裡先應用synchronized鎖住對象o2 */
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1) {
/*
* 後面曾經鎖住了對象o2,只需再能鎖住o1,那末就可以履行打印出0的操作了 可是這裡沒法鎖定對象o1,由於在別的一個flag=1這個線程外面曾經把對象o1給鎖住了 雖然鎖住o1這個對象的線程會每隔500毫秒睡眠一次,可是在睡眠的時刻依然是鎖住o1不放的
*/
System.out.println("0");
}
}
}
}
public static void main(String args[]) {
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.setName("線程td1");
t2.setName("線程td2");
t1.start();
t2.start();
}
}
處理線程逝世鎖的成績最好只鎖定一個對象,不要同時鎖定兩個對象
臨盆者花費者成績:
package cn.galc.test;
/* 典范稱號:臨盆者--花費者成績
* 源文件稱號:ProducerConsumer.java
* 要 點:
* 1. 同享數據的紛歧致性/臨界資本的掩護
* 2. Java對象鎖的概念
* 3. synchronized症結字/wait()及notify()辦法
*/
public class ProducerConsumer {
public static void main(String args[]){
SyncStack stack = new SyncStack();
Runnable p=new Producer(stack);
Runnable c = new Consumer(stack);
Thread p1 = new Thread(p);
Thread c1 = new Thread(c);
p1.start();
c1.start();
}
}
class SyncStack{ //支撐多線程同步操作的客棧的完成
private int index = 0;
private char []data = new char[6];
public synchronized void push(char c){
if(index == data.length){
try{
this.wait();
}catch(InterruptedException e){}
}
this.notify();
data[index] = c;
index++;
}
public synchronized char pop(){
if(index ==0){
try{
this.wait();
}catch(InterruptedException e){}
}
this.notify();
index--;
return data[index];
}
}
class Producer implements Runnable{
SyncStack stack;
public Producer(SyncStack s){
stack = s;
}
public void run(){
for(int i=0; i<20; i++){
char c =(char)(Math.random()*26+'A');
stack.push(c);
System.out.println("produced:"+c);
try{
Thread.sleep((int)(Math.random()*1000));
}catch(InterruptedException e){
}
}
}
}
class Consumer implements Runnable{
SyncStack stack;
public Consumer(SyncStack s){
stack = s;
}
public void run(){
for(int i=0;i<20;i++){
char c = stack.pop();
System.out.println("花費:"+c);
try{
Thread.sleep((int)(Math.random()*1000));
}catch(InterruptedException e){
}
}
}
}
以上就是關於java線程的全體內容引見,年夜家可以聯合第一篇《java必學必會之線程(1)》停止進修,願望可以贊助到年夜家。