package 多线程;
/*
* 线程优先级高的获取的CPU时间片相对多一些
*
* 优先级:1 - 10
* 最低 1
* 最高 10
* 默认是 5
*/
public class ThreadTest05
{
public static void main(String[] args)
{
Thread t1 = new Procesor();
t1.setName("t1");
Thread t2 = new Procesor();
t2.setName("t2");
System.out.println(t1.getPriority());
System.out.println(t2.getPriority());
//设置优先级
t1.setPriority(4);
t2.setPriority(6);
//启动线程
t1.start();
t2.start();
}
}
class Procesor extends Thread
{
public void run()
{
for(int i = 0; i < 50; i ++)
{
System.out.println(Thread.currentThread().getName()+"---->"+ i);
}
}
}
package 多线程;
/*
* 1.Thread.sleep(毫秒);
* 2.sleep 方法是一个静态方法
* 3.该方法的作用:阻塞当前线程,腾出CPU,让给其他线程
*/
public class ThreadTest06
{
public static void main(String[] args) throws InterruptedException
{
Thread t1 = new Proceor();
t1.setName("t");
t1.start();
//阻塞主线程
for(int i = 0; i < 10; i ++)
{
System.out.println(Thread.currentThread().getName()+"---->"+i);
Thread.sleep(500);
}
}
}
class Proceor extends Thread
{
//Thread中的run方法不抛出异常。所以出重写run方法之后,在run方法的声明位置上不能使用throws
//所以run方法中的异常只能try--catch--
public void run()
{
for(int i = 0; i < 10; i ++) {
System.out.println(Thread.currentThread().getName()+"--->"+ i);
try{
Thread.sleep(1000);//让当前线程阻塞1秒
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
package 多线程;
public class ThreadTest07
{
public static void main(String[] args) {
Thread t1 = new Procer();
t1.setName("t1");
t1.start();
try {
t1.sleep(1000);//同等于Thread.sleep(1000); 与t线程无关
}catch(InterruptedException e) {
e.printStackTrace();
}
A a = null;
a.m1();//不会报空指针异常。(静态方法执行和对象无关)
}
}
class Procer extends Thread
{
public void run() {
for(int i = 0; i < 5; i ++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
try {
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class A
{
public static void m1() {}
}
package 多线程;
/*
* 某线程正在休眠,打断它的休眠
*
* 以下方式依靠的是异常处理机制
*/
public class ThreadTest08
{
public static void main(String[] args) throws Exception{
//需求,线程启动5秒之后,打断休眠
Thread t = new Thread(new processorr());
//起名
t.setName("t");
//启动
t.start();
//计时
t.sleep(5000);
//中断
t.interrupt();
}
}
class processorr implements Runnable
{
public void run() {
try {
Thread.sleep(10000000);
// System.out.println("HhelloWorld");这段代码不会执行
}catch(InterruptedException e) {
e.printStackTrace();
}
for(int i = 0; i < 10; i ++) {
System.out.println(Thread.currentThread().getName()+"-->"+ i);
}
}
}
package 多线程;
/*
* 如何正确的终止一个正在执行的程序
*
* 需求:5秒之后终止
*/
public class ThreadTest09
{
public static void main(String[] args) throws Exception
{
Prooessor p = new Prooessor();
Thread t = new Thread(p);
t.setName("t");
t.start();
t.sleep(5000);
p.run = false;
}
}
class Prooessor implements Runnable
{
Boolean run = true;
public void run() {
for(int i = 0; i <10; i ++){
if(run) {
try {
Thread.sleep(1000);
}catch(Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
}
}
package 多线程;
/*
* Thread.yield();
* 1.该方法是一个静态方法
* 2.作用:给同一个优先级的线程让位,但是让位时间不确定
* 3.和sleep方法相同,但是yield方法让位时间不固定
*/
public class ThreadTest10
{
public static void main(String[] args)
{
Thread t = new Pro();
t.setName("t");
t.start();
for(int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
}
class Pro extends Thread
{
public void run()
{
for(int i = 0; i < 100; i ++) {
System.out.println(Thread.currentThread().getName()+"-->"+i);
if(i%20 == 0) {
Thread.yield();
}
}
}
}
package 多线程;
/*
* 线程的合并
*/
public class ThreadTest11
{
public static void main(String[] args) throws Exception
{
Thread t = new Thread(new Proc());
t.setName("t");
t.start();
//合并线程
t.join();//t和主线程合并
for(int i = 0; i < 10; i ++) {
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
}
class Proc implements Runnable
{
public void run()
{
for(int i = 0; i < 10; i ++) {
System.out.println(Thread.currentThread().getName()+"-->"+i);
}
}
}
package 多线程;
//以下程序使用线程同步机制保证数据的安全
public class ThreadTest13
{
public static void main(String[] args)
{
//创建一个公共的账户
Account1 act = new Account1("actno-001",5000.0);
//创建线程对同一个账户取款
pors1 p = new pors1(act);
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
t1.start();
t2.start();
}
}
//取款线程
class pors1 implements Runnable
{
//账户
Account1 act;
//构造方法(Constructor)
pors1 (Account1 act)
{
this.act = act;
}
public void run()
{
act.withdraw(1000.0);
System.out.println("取款1000.0,余额:"+act.getBalance());
}
}
//账户
class Account1
{
private String actno;
private double balance;
public Account1() {}
public Account1(String actno, double balance)
{
this.actno = actno;
this.balance = balance;
}
//set和get 方法
public void setActno(String actno)
{
this.actno = actno;
}
public String getActno()
{
return actno;
}
public void setBalance(double balance)
{
this.balance = balance;
}
public double getBalance()
{
return balance;
}
//对外提供一个取款的方法
//public synchronized void withdraw(double money)
public void withdraw(double money)
{//对当前账户进行取款
//把需要同步的代码放到同步语句块中
/*
* 原理:t1线程执行到此处,遇到了synchronized关键字。就会去找this对象锁
* 如果找到this对象锁,则进入同步语句块中执行程序。当同步语句块中的代码执行结束
* 之后,t1线程归还this对象锁
*
* 在t1线程执行同步语句块的过程中,如果t2线程也过来执行以下代码,也遇到synchronized关键字,
* 所以也去找this的对象锁,但是该对象锁被t1线程所持有,只能在这等待this对象的归还
*/
synchronized(this/*共享对象*/)
{
double after = balance - money;
try {Thread.sleep(1000);}catch(InterruptedException e) {}
//更新
this.setBalance(after);
}
}
}