Java学习-关于多线程-1

齐头并进完成任务-----多线程

-了解进程和线程

-掌握实现多线程方法

-熟悉线程的状态

-熟悉线程操作的方法

-线程同步的方法

-线程间通信方法


-了解进程和线程

==》进程是一个执行的程序,每一个进程偶有自己独立的内存空间,一组系统资源:    内部数据和状态都是独立!

         多线程是单个进程中,同时运行多个不同线程,执行不同任务(共享代码和数据):  多线程意味着一个程序的多行语句可以看上去同时运行!

总之:利用多线程可以使编程人员方便的开发出能同事处理多个任务的功能强大的应用程序


-认识线程

传统遇到if else,  while ,for循环多绕几个圈,一次只能运行一个程序块,多线程可以在一个程序块时,同时运行其他程序块以提高效率!

---------------------------------------------------------------------------

public class _1901 {

public static void main(String[] args) {
new TestThread().run();
for(int i = 0; i < 10; i++){
System.out.println("main线程在运行");
}
}
}

class TestThread{
public void run(){
for(int i = 0; i < 10; i++){
System.out.println("TestThread在运行");
}
}
}

必须要等TestThread中run()结束后才可以进行Main线程,怎么才能做到main和TestThread同时进行?

---------------------------------------------------------------------------

命名方法:继承Thread类

class 类名称 extends Thread{

属性;

方法;

修饰符 run{

}

}

---------------------------------------------------------------------------同时激活多个线程

public class _1902 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestThread4().start();
for(int i = 0; i < 10; i++){
System.out.println("main线程在运行");
}
}
}

class TestThread4 extends Thread{
public void run(){
for(int i = 0; i < 10; i++){
System.out.println("TestThread在运行");
}
}
}

想使用多线程,必须调用Thread类中start()方法,也就相当于调用run()方法

---------------------------------------------------------------------------

-命名方法:实现接口Runnable

class 类名称 implements Runnable

{

属性;

方法;

修饰符 run(){

}

}

---------------------------

public class _1903 {
public static void main(String[] args) {
TestThread5 t = new TestThread5();
new Thread(t).start();

}
}

class TestThread5 implements Runnable{
public void run(){
for(int i = 0; i < 10; i++){
System.out.println("TestThread在运行");
}
}
}

Runnable 接口中无start()方法。  Thread类中的一个构造方法: public Thread(Runnable target)

将Runnable实例化对象作为参数去实例化Thread类对象。

--------------------------------------

-extends Thread 与 implements Runnable 之间的区别

thread类实现了Runnable接口,也就是说Thread类也是Runnable接口的一个子类

---------------------------------------------------------------------------

Thread类继承实例-1

public class _1904 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread6 t = new TestThread6();
t.start();
t.start();
t.start();
t.start();
}
}

class TestThread6 extends Thread{
private int tickets = 20;
public void run(){
while(true){
if(tickets > 0){
System.out.println(Thread.currentThread().getName() + "出售票" + tickets--);
}
}
}
}

只执行其中一个t.start()程序

---------------------------------------------------------------------------

Thread类继承实例-2

public class _1905 {
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestThread7().start();
new TestThread7().start();
new TestThread7().start();
new TestThread7().start();
}
}

class TestThread7 extends Thread{
private int tickets = 20;
public void run(){
while(true){
if(tickets > 0){
System.out.println(Thread.currentThread().getName() + "出售票" + tickets--);
}
}
}
}

启动了4个线程,但是4个线程各自占有各自的资源,得出结论:用Thread类实际上无法达到资源共享的目的

---------------------------------------------------------------------------

用Runnable接口实现多线程使用实例

public class _1906 {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestThread8 t = new TestThread8();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
new Thread(t).start();
}
}

class TestThread8 implements Runnable{
private int tickets = 20;
public void run(){
while(true){
if(tickets > 0){
System.out.println(Thread.currentThread().getName() + "出售票" + tickets--);
}
}
}
}

4个线程同时进行且对票的资源进行了共享,无重复票数出现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值