线程的优先级(setPriority)

线程可以划分优先级,优先级高的线程得到的CPU资源比较多,也就是CPU优先执行优先级高的线程对象中的任务。

设置线程优先级有助于帮助线程规划器确定下一次选中哪一个线程优先执行。

java中优先级分为1-10个级别

线程优先级的继承特性 例如a线程启迪b线程,则b线程的优先级与a的一样。

代码说话:(很简单)

public class MyThread1 extends Thread {
	@Override
	public void run() {
		System.out.println("MyThread1 run priority=" + this.getPriority());
		MyThread2 thread2 = new MyThread2();
		thread2.start();
	}
}


public class MyThread2 extends Thread {
	@Override
	public void run() {
		System.out.println("MyThread2 run priority=" + this.getPriority());
	}
}

public static void main(String[] args) {
		System.out.println("main thread begin priority="
				+ Thread.currentThread().getPriority());
		
		Thread.currentThread().setPriority(6);
		
		System.out.println("main thread end   priority="
				+ Thread.currentThread().getPriority());
		
		MyThread1 thread1 = new MyThread1();
		thread1.start();
	}

优先级具有规则性
public class MyThread1 extends Thread {
	@Override
	public void run() {
		long beginTime = System.currentTimeMillis();
		long addResult = 0;
		for (int j = 0; j < 10; j++) {
			for (int i = 0; i < 50000; i++) {
				Random random = new Random();
				random.nextInt();
				addResult = addResult + i;
			}
		}
		long endTime = System.currentTimeMillis();
		System.out.println("★★★★★thread 1 use time=" + (endTime - beginTime));
	}
}

public class MyThread2 extends Thread {
	@Override
	public void run() {
		long beginTime = System.currentTimeMillis();
		long addResult = 0;
		for (int j = 0; j < 10; j++) {
			for (int i = 0; i < 50000; i++) {
				Random random = new Random();
				random.nextInt();
				addResult = addResult + i;
			}
		}
		long endTime = System.currentTimeMillis();
		System.out.println("☆☆☆☆☆thread 2 use time=" + (endTime - beginTime));
	}
}

public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			MyThread1 thread1 = new MyThread1();
			thread1.setPriority(1);
			thread1.start();

			MyThread2 thread2 = new MyThread2();
			thread2.setPriority(10);
			thread2.start();
		}
	}

高优先级的线程总是先执行完

线程的优先级和代码的执行顺序没有关系


优先级具有随机性

一般优先级较高的线程先执行run()方法,但是这个不能说的但肯定,因为线程的优先级具有 “随机性”也就是较高线程不一定每一次都先执行完。

public class MyThread1 extends Thread {
	@Override
	public void run() {
		long beginTime = System.currentTimeMillis();
		for (int i = 0; i < 1000; i++) {
			Random random = new Random();
			random.nextInt();
		}
		long endTime = System.currentTimeMillis();
		System.out.println("★★★★★thread 1 use time=" + (endTime - beginTime));
	}
}

public class MyThread2 extends Thread {
	@Override
	public void run() {
		long beginTime = System.currentTimeMillis();
		for (int i = 0; i < 1000; i++) {
			Random random = new Random();
			random.nextInt();
		}
		long endTime = System.currentTimeMillis();
		System.out.println("☆☆☆☆☆thread 2 use time=" + (endTime - beginTime));
	}
}


public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			MyThread1 thread1 = new MyThread1();
			thread1.setPriority(5);
			thread1.start();

			MyThread2 thread2 = new MyThread2();
			thread2.setPriority(6);
			thread2.start();
		}
	}


守护线程介绍:

java 中有两种线程 一个是用户线程,一个是守护(Daemon)线程

典型的守护线程就是垃圾回收线程,如果进程中没有非守护线程了,则守护线程自动销毁。

守护线程作用就是为其他线程的运行提供便利的服务,比如GC。

public class MyThread extends Thread {
	private int i = 0;

	@Override
	public void run() {
		try {
			while (true) {
				i++;
				System.out.println("i=" + (i));
				Thread.sleep(1000);
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


public static void main(String[] args) {
		try {
			MyThread thread = new MyThread();
			thread.setDaemon(true);//设置守护线程
			thread.start();
			Thread.sleep(5000);
			System.out.println("我离开thread对象也不再打印了,也就是停止了!");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
线程与智能指针 C++线程与智能指针 ⽂章⽬录 线程 线程,有时被称为轻量进程,是程序执⾏的最⼩单元。 C++11线程 #include <thread> void task(int i) { cout << "task:" << i << endl; } thread t1(task,100); //等待线程结束再继续执⾏ t1.join(); POSIX线程 POSIX 可移植操作系统接⼝,标准定义了操作系统应该为应⽤程序提供的接⼝标准 Windows上使⽤ 配置: cmake_minimum_required (VERSION 3.8) include_directories("XXX/pthreads-w32-2-9-1-release/Pre-built.2/include") #设置变量为x64 or x86 if(CMAKE_CL_64) set(platform x64) else() set(platform x86) endif() link_directories("XXX/pthreads-w32-2-9-1-release/Pre-built.2/lib/${platform}") #如果出现 "timespec":"struct" 类型重定义 设置 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_STRUCT_TIMESPEC") # 将源添加到此项⽬的可执⾏⽂件。 add_executable (lsn6example "lsn6_example.cpp" "lsn6_example.h") target_link_libraries(lsn6example pthreadVC2) 32位拷贝pthreadVC2.dll 到windows/syswow64⽬录 64位拷贝pthreadVC2.dll 到windows/system32⽬录 #include <pthread.h> void *pthreadTask(void* args) { int* i = static_cast<int*>(args); cout << "posix线程:" << *i << endl; return 0; } pthread_t pid; int pi = 100; pthread_create(&pid, 0, pthreadTask, &pi); //等待线程的结束 pthread_join(pid,0); 线程属性 线程具有属性,⽤ pthread_attr_t 表⽰ pthread_attr_t attr; //初始化 attr中为操作系统实现⽀持的线程所有属性的默认值 pthread_attr_init(&attr); pthread_attr_destroy(&attr); 分离线程 线程创建默认是⾮分离的,当pthread_join()函数返回时,创建的线程终⽌,释放⾃⼰占⽤的系统资源 分离线程不能被其他线程等待,pthread_join⽆效,线程⾃⼰玩⾃⼰的。 //设置是否为分离线程 //PTHREAD_CREATE_DETACHED 分离 //PTHREAD_CREATE_JOINABLE ⾮分离 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); 调度策略与优先级 Windows ⽆法设置成功 //设置调度策略 //返回0 设置成功 pthread_attr_setschedpolicy(&attr, SCHED_FIFO); // SCHED_FIFO // 实时调度策略,先到先服务 ⼀旦占⽤cpu则⼀直运⾏。⼀直运⾏直到有更⾼优先级任务到达或⾃⼰放弃。 // SCHED_RR // 实时调度策略,时间轮转 系统分配⼀个时间段,在时间段内执⾏本线程 //设置优先级 //获得对应策略的最⼩、最⼤优先级 int max = sched_get_priority_max(SCHED_FIFO); int min = sched_get_priority_min(SCHED_FIFO); sched_param param; param.sched_priority = max; pthread_attr_setschedparam(&attr, &param); 线程同步 多线程同时读写同⼀份共享资源的时候,可能会引起冲突。需要引⼊线程"同步"机制,即各位线程之间有序地对共享资源进⾏操 作。 #include <pthread.h> using namespace std; queue<int> q; void *pop(void* args) { //线程未同步导致的多线程

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值