疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Java线程的基本概念和操作


 

   Java线程的基本概念和操作

  一:线程的基本概念

  1.调用run()方法

  public class Demo1 {

  publicstatic void main(String[] args) {

  Thread1th1=new Thread1("线程A");

  Thread1th2=new Thread1("线程B");

  th1.run();

  th2.run();

  }

  }

  class Thread1 extends Thread{

  privateString name;

  publicThread1(String name){

  this.name=name;

  }

  publicvoid run(){

  for(inti=0;i<10;i++){

  System.out.println(name+"运行i="+i);

  }

  }

  }

  2.调用start()方法,报错

  Thread1 th1=new Thread1("线程A");

  th1.start();

  th1.start();

  3.实现runnable接口

  public class Demo1 {

  publicstatic void main(String[] args) {

  Thread1th1=new Thread1("线程A");

  Threadt1=new Thread(th1);

  t1.start();

  }

  }

  class Thread1 implements Runnable{

  privateString name;

  publicThread1(String name){

  this.name=name;

  }

  publicvoid run(){

  for(inti=0;i<10;i++){

  System.out.println(name+"运行i="+i);

  }

  }

  }

  总结:实现Runnable接口的优势

  1.多个相同的程序代码线程处理同一资源

  2.避免java单继承带来的局限

  3.增强程序的健壮性

  二:线程的状态

  1.创建状态

  Thread thread=newThread();

  2.就绪状态

  thread.start();

  3.运行状态

  thread.run();

  4.堵塞状态

  sleep(),wait(),suspend()

  5.死亡状态

  thread.stop();或者run()方法运行结束后

  操作线程的相关方法

  1.线程名称

  package com.cloud.day5;

  public class day1 {

  publicstatic void main(String[] args) {

  MyThreadmt=new MyThread();

  //系统自动设置线程名称

  newThread(mt).start();

  //自己设置线程名称

  newThread(mt,"线程A").start();

  }

  }

  class MyThread implements Runnable{

  @Override

  publicvoid run() {

  for(inti=0;i<3;i++){

  System.out.println(Thread.currentThread().getName()+"运行i="+i+"次");

  }

  }

  }

  2.判断线程是否启动

  package com.cloud.day5;

  public class day1 {

  publicstatic void main(String[] args) {

  MyThreadmt=new MyThread();

  Threadt=new Thread(mt,"线程");

  System.out.println("线程启动前:"+t.isAlive());

  t.start();

  System.out.println("线程启动后:"+t.isAlive());

  for(inti=0;i<3;i++){

  System.out.println("main运行"+i);

  }

  System.out.println("代码执行完毕"+t.isAlive());

  }

  }

  class MyThread implements Runnable{

  @Override

  publicvoid run() {

  for(inti=0;i<3;i++){

  System.out.println(Thread.currentThread().getName()+"运行i="+i+"次");

  }

  }

  }

  3.线程强制运行

  package com.cloud.day5;

  public class day1 {

  publicstatic void main(String[] args) {

  MyThreadmt=new MyThread();

  Threadt=new Thread(mt,"线程");

  t.start();

  for(inti=0;i<50;i++){

  if(i>10){

  try{

  t.join();

  }catch(Exceptione){}

  }

  System.out.println("main线程运行"+i);

  }

  }

  }

  class MyThread implements Runnable{

  @Override

  publicvoid run() {

  for(inti=0;i<3;i++){

  System.out.println(Thread.currentThread().getName()+"运行i="+i+"次");

  }

  }

  }

  4.线程休眠

  package com.cloud.day5;

  public class day1 {

  publicstatic void main(String[] args) {

  MyThreadmt=new MyThread();

  Threadt=new Thread(mt,"线程");

  t.start();

  }

  }

  class MyThread implements Runnable{

  @Override

  publicvoid run() {

  for(inti=0;i<5;i++){

  try{

  Thread.sleep(500);

  }catch (Exception e) {

  }

  System.out.println(Thread.currentThread().getName()+"运行i="+i+"次");

  }

  }

  }

  5.线程的中断

  package com.cloud.day5;

  public class day1 {

  publicstatic void main(String[] args) {

  MyThreadmt=new MyThread();

  Threadt=new Thread(mt,"线程");

  t.start();

  try{

  Thread.sleep(2000);

  }catch (Exception e) {

  //TODO: handle exception

  }

  //中断线程执行

  t.interrupt();

  }

  }

  class MyThread implements Runnable{

  @Override

  publicvoid run() {

  System.out.println("1.进入run方法");

  try{

  Thread.sleep(10000);

  System.out.println("2.完成10秒休眠");

  }catch (Exception e) {

  System.out.println("3.终止休眠");

  }

  System.out.println("4.run方法正常结束");

  }

  }

  6.线程的优先级

  package com.cloud.day5;

  public class day1 {

  publicstatic void main(String[] args) {

  System.out.println("main线程的优先级:"+Thread.currentThread().getPriority());

  Threadt1=new Thread(new MyThread(),"线程A");

  Threadt2=new Thread(new MyThread(),"线程B");

  Threadt3=new Thread(new MyThread(),"线程C");

  t1.setPriority(Thread.MIN_PRIORITY);

  t2.setPriority(Thread.MAX_PRIORITY);

  t3.setPriority(Thread.NORM_PRIORITY);

  t1.start();

  t2.start();

  t3.start();

  }

  }

  class MyThread implements Runnable{

  @Override

  publicvoid run() {

  for(inti=0;i<5;i++){

  try{

  Thread.sleep(500);

  }catch (Exception e) {

  }

  System.out.println(Thread.currentThread().getName()+"运行i="+i+"次");

  }

  }

  }