多线程理解,线程
程序员文章站
2022-07-05 23:43:36
...
最近在玩安卓,要提到多线程, 总感觉那东西有点不明白,总是有那些停止,暂停,销毁,启动什么的,总是执行那些程序,先后,快慢,说实话,不知道那是什么意思的,
个人感觉多线程并不是什么情况下都适用的,一般用在定时功能,用得最好的只有播放器,听音乐的这些软件了, 有快进,有慢,有暂停,关闭这些都是多线程的,多线程的意义不在于共同执行一个任务或者是一段代码什么的, 他的意义在于,控制多个线程来完成一个任务,有选择的来完成,
不知道理解错了没有,也就是自己的理解吧,确实不太明白
贴出多线程例子,看意思代码分析
package com.pu.gc;
public class TestThread{
public static void main(String[] args) {
//创建饼的实例
Bing b = new Bing();
//创建同步的吃饼和烙饼实例
GongNeng g = new GongNeng(b);
//创建线程两个消费者两个生产者
ChuShi c1 = new ChuShi(g);
ChuShi c2 = new ChuShi(g);
XiaoFei x1 = new XiaoFei(g);
XiaoFei x2 = new XiaoFei(g);
//启动线程
c1.start();
c2.start();
x1.start();
x2.start();
}
}
class ChuShi extends Thread{
private GongNeng g;
public ChuShi(GongNeng g){
this.g = g;
}
public void run(){
for(;;){
g.lao();
}
}
}
class XiaoFei extends Thread{
private GongNeng g;
public XiaoFei(GongNeng g){
this.g = g;
}
public void run(){
for(;;){
g.chi();
}
}
}
class GongNeng {
private Bing b;
public GongNeng(Bing b){
this.b = b;
}
public synchronized void lao(){
if(b.getBing()<10){
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread()+
":厨师烙了一张饼,盘里还剩"+(b.getBing()+1)+"张饼");
b.setBing(b.getBing()+1);
}
}
public synchronized void chi(){
if(b.getBing()>0){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+
":消费者吃了一张饼,盘里还剩"+(b.getBing()-1)+"张饼");
b.setBing(b.getBing()-1);
}
}
}
class Bing {
private int bing = 10;
public int getBing() {
return bing;
}
public void setBing(int bing) {
this.bing = bing;
}
}
个人感觉多线程并不是什么情况下都适用的,一般用在定时功能,用得最好的只有播放器,听音乐的这些软件了, 有快进,有慢,有暂停,关闭这些都是多线程的,多线程的意义不在于共同执行一个任务或者是一段代码什么的, 他的意义在于,控制多个线程来完成一个任务,有选择的来完成,
不知道理解错了没有,也就是自己的理解吧,确实不太明白
贴出多线程例子,看意思代码分析
package com.pu.gc;
public class TestThread{
public static void main(String[] args) {
//创建饼的实例
Bing b = new Bing();
//创建同步的吃饼和烙饼实例
GongNeng g = new GongNeng(b);
//创建线程两个消费者两个生产者
ChuShi c1 = new ChuShi(g);
ChuShi c2 = new ChuShi(g);
XiaoFei x1 = new XiaoFei(g);
XiaoFei x2 = new XiaoFei(g);
//启动线程
c1.start();
c2.start();
x1.start();
x2.start();
}
}
class ChuShi extends Thread{
private GongNeng g;
public ChuShi(GongNeng g){
this.g = g;
}
public void run(){
for(;;){
g.lao();
}
}
}
class XiaoFei extends Thread{
private GongNeng g;
public XiaoFei(GongNeng g){
this.g = g;
}
public void run(){
for(;;){
g.chi();
}
}
}
class GongNeng {
private Bing b;
public GongNeng(Bing b){
this.b = b;
}
public synchronized void lao(){
if(b.getBing()<10){
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread()+
":厨师烙了一张饼,盘里还剩"+(b.getBing()+1)+"张饼");
b.setBing(b.getBing()+1);
}
}
public synchronized void chi(){
if(b.getBing()>0){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+
":消费者吃了一张饼,盘里还剩"+(b.getBing()-1)+"张饼");
b.setBing(b.getBing()-1);
}
}
}
class Bing {
private int bing = 10;
public int getBing() {
return bing;
}
public void setBing(int bing) {
this.bing = bing;
}
}
上一篇: JQ filter() 过滤函数
下一篇: JQ 全选和反选和取消