什么是线程???
进程的概念:进程是指可执行程序并存放在计算机存储器的一个指令序列,他是一个动态执行的过程。 进程的概念和特征_C语言中文网 (biancheng.net) 线程是比进程还要小的运行单位,一个进程含多个线程
线程创建
Thread
类,或者一个Thread
子类的对象Runnable
接口的类的对象Thread
是一个线程类,位于java.lang包下Thread
类的常用方法run();
Runnable
是Java中用以实现线程的接口class MyThread extends Thread{
public void run(){
System.out.println(getName()+"该线程正在执行!");
}
}
/**
* 线程是随机运行的且只能调用一次start方法。
* 多次调用会报异常 java.lang.IllegalThreadStateException
*/
public class ThreadTest {
public static void main(String[] args) {
System.out.println("主线程1");
MyThread mt=new MyThread();
mt.start();//调用start启动线程
System.out.println("主线程2");
}
}
class MyThread extends Thread{
public MyThread(String name){
super(name);
}
public void run(){
for (int i=1;i<=10;i++){
System.out.println(getName()+"该线程已运行"+i+"次");
}
}
}
public class ThreadTest {
public static void main(String[] args) {
MyThread mt1=new MyThread("线程1");
mt1.start();
MyThread mt2=new MyThread("线程2");
mt2.start();
}
}
Runnable
接口?Thread
类其他的方法Runnable是接口不能直接调用Thread类的方法
/**
* 用Thread类调用currentThread()也就是当前线程,再去调用getName()方法
*/
class PrintRunnable implements Runnable{
@Override
public void run(){
int i=1;
while(i<=5) {
System.out.println(Thread.currentThread().getName() + "正在运行"+(i++));
}
}
}
/**
* 此处不能直接调用strat方法,因为根本没有此方法
*/
public class Test {
public static void main(String[] args) {
PrintRunnable pr=new PrintRunnable();
Thread t1=new Thread(pr);
t1.start();
PrintRunnable pr1=new PrintRunnable();
Thread t2=new Thread(pr1);
t2.start();
}
}
class PrintRunnable implements Runnable{
int i=1;
@Override
public void run(){
while(i<=5) {
System.out.println(Thread.currentThread().getName() + "正在运行"+(i++));
}
}
}
public class Test {
public static void main(String[] args) {
/**
* 此时定义的int i=1; 相当于一个资源被t1和t2共享,
* 这种情况适合多个线程处理同一个资源
*/
PrintRunnable pr=new PrintRunnable();
Thread t1=new Thread(pr);
t1.start();
// PrintRunnable pr1=new PrintRunnable();
Thread t2=new Thread(pr);
t2.start();
}
}
Thread
类的方法public static void sleep(long millis)
class MyThread implements Runnable{
public void run(){
for (int i = 1; i <=15 ; i++) {
System.out.println(Thread.currentThread().getName()+"运行了"+i+"次");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public class SleepDemo {
public static void main(String[] args) {
MyThread mt=new MyThread();
Thread t=new Thread(mt);
t.start();
}
}
Thread
类的方法public final void join()
public final void join(long millis)
class MyThread extends Thread{
public void run(){
for (int i = 1; i < 400; i++) {
System.out.println(getName()+"正在执行"+i+"次");
}
}
}
public class JoinDemo {
public static void main(String[] args) {
MyThread mt=new MyThread();
mt.start();
try {
mt.join(1);//不管运行多少次,在1毫秒后都会停止
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
for (int i=1;i<10;i++){
System.out.println("主线程运行第"+i+"次");
}
System.out.println("主线程结束");
}
}
MAX_PRIORITY
:线程的最高优先级10MIN_PRIORITY
:线程的最低优先级1NORM_PRIORITY
:线程的默认优先级5class MyThread extends Thread{
private String name;
public MyThread(String name){
this.name=name;
}
@Override
public void run() {
for (int i = 1; i <=10; i++) {
System.out.println("线程"+name+"正在运行"+i);
}
}
}
public class Priority {
public static void main(String[] args) {
//获取主线程的优先级
int mainPriority=Thread.currentThread().getPriority();
System.out.println("主线程优先级:"+mainPriority);
MyThread mt1=new MyThread("线程1");
MyThread mt2=new MyThread("线程2");
//mt1.setPriority(10);
mt1.setPriority(Thread.MAX_PRIORITY);
mt2.setPriority(Thread.MIN_PRIORITY);
mt1.start();
mt2.start();
//System.out.println("线程1的优先级:"+mt1.getPriority());
}
}
synchronized
void saveAccount(){}synchronized
void saveAccount(){}synchronized
(obj){……}wait()
方法:中断方法的执行,使线程等待notify()
方法:唤醒处于等待的某一个线程,使其结束等待notifyAll()
方法:唤醒所有处于等待的线程,使其结束等待扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有