线程优先级:
线程虽然有优先级,但是并不能保证一定先执行,只是为优先执行提供可能。
优先级范围 :1—10 数字越大,优先级越高。
public class Demo extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName()+"在执行!");
}
public static void main(String[] args) {
Demo d1 = new Demo();
d1.setName("线程一");
// d1.setPriority(10);
// d1.setPriority(MAX_PRIORITY);
d1.start();
Demo d2 = new Demo();
d2.setName("线程二");
// d2.setPriority(MIN_PRIORITY);
d2.start();
Demo d3 = new Demo();
d3.setName("线程三");
d3.start();
System.out.println(Thread.currentThread().getName()+"在执行!=========");
}
}
public class MyRunnable1 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 500; i++) {
System.out.println((i+1)+":1===线程");
}
}
}
package d87_thread_1;
public class MyRunnable2 implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 500; i++) {
System.out.println((i+1)+":第二个线程在执行");
}
}
}
package d87_thread_1;
public class MyRunTest {
public static void main(String[] args) {
MyRunnable1 my1 = new MyRunnable1();
MyRunnable2 my2 = new MyRunnable2();
Thread t1 = new Thread(my1);
Thread t2 = new Thread(my2);
// t1.setPriority(Thread.MAX_PRIORITY);;
t1.start();
t2.start();
}
}
线程休眠----------------------------------------------------------------
package Test_Thread;
public class Thread1 implements Runnable{
@Override
public void run() {
for (int i = 0;i<20;i++){
System.out.println(Thread.currentThread().getName()+i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread1 t = new Thread1();
Thread f = new Thread(t);
f.start();
}
}
线程休眠
package Test_Thread;
public class Thread2 implements Runnable {
@Override
public void run() {
for (int i = 0;i<50;i++){
System.out.println(Thread.currentThread().getName()+"--->"+i);
if(i%4==0){
Thread.yield();
}
}
}
public static void main(String[] args) {
Thread2 t2 = new Thread2();
Thread x = new Thread(t2);
Thread p = new Thread(t2);
x.setName("线程A");
p.setName("线程B");
x.start();
p.start();
}
}
线程的同步:
但过多的同步会产生死锁。
Synchronized 关键字可修饰 同步代码块和同步方法
package Test_Thread.savemoney;
public class Money {
public static int money = 50;
}
package Test_Thread.savemoney;
public class Getmoney extends Money implements Runnable {
public synchronized void getmoney(){
if(money>0) {
money -= 10;
System.out.println("取出10元,剩余" + money + "元");
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (money<300){
this.getmoney();
}
}
}
package Test_Thread.savemoney;
public class Save extends Money implements Runnable {
public synchronized void savemoney() {
money += 20;
System.out.println("存入20元,剩余" + money + "元");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
while (money<300){
this.savemoney();
}
}
}
--------------------------------------------------------------------------
package Test_Thread.savemoney;
public class Test {
public static void main(String[] args) {
Save s = new Save();
Thread t1 = new Thread(s);
Getmoney g = new Getmoney();
Thread t2 = new Thread(g);
t1.start();
t2.start();
}
}