python学习之异常与多线程

异常:在java中,有运行时异常和检查异常,因为它是半编译半解释型语言;在python中,只有运行时异常,因为它是纯解释型语言。

异常的种类和体系结构 
Throwtable
Error---不能回复的异常
Exception
检查异常---所有的Exception子类,所有的非RuntimeException的子类

运行时异常---所有的RuntimeException的子类


主动throw一个运行时异常
throw new RuntimeException("运行时异常");
主动throw一个检查时异常
throw new Exception("检查时异常");


小练习:只使用一行代码就能报异常 

System.in.read();     不使用try或throws会报语法错误:

args[1]="hehe"   数组越界(未传参数)


举个栗子:

num1=int(input("整数"))
num2=int(input("整数"))
try:
shang=num1/num2
print("{0}/{1}={2}".format(num1,num2,shang))
方法1:父类方式
 except Exception as a :
     print(a)

方法2: 多个except
except TypeError as eo:
    print("有错误了:{0}".format(eo))
except ZeroDivisionError as eo:
    print("有错误了:{0}".format(eo))
 finally:
    print("感谢使用")

#方法3:写在一起
except (TypeError , ZeroDivisionError) as t:
print("有错误了:{0}".format(t))

finally:
print("感谢使用")


****************************************************************************************************************************************************

多线程:Java中,多线程实现方式主要有三种:继承Thread类,实现Runnable接口,使用ExecutorService、 Callable、Future实现有返回结果的多线程。
                              其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。
 线程操作涉及到的方法:start    stop(过时的方法)    sleep    join    wait    notify    notifyAll


在python中,多线程实现方式有两种方式: 类   函数
  线程操作涉及到的方法
对象: (类:方法)
Thread:start启动  join加入新线程
Lock、Rlock:acquire加锁  release释放锁
Condition:acquire加锁  release释放锁  wait  notify  notify_all
Event:set(相当于notify_all)  clear(取消notify_all)
timer: sleep(跟java中sleep方法一样)
Timer(interval,function,args=[],kwargs={})
interval: 指定的时间
function:要执行的方法
args/kwargs:方法的参数

PS:Lock和RLock的区别:Lock锁过一次在锁就会产生死锁;RLock锁过一次在锁不会堵塞


接下来大家来吃馒头吧^_^~

/**
共有四个老和尚,其中一个
和尚负责做馒头
其他三个老和尚吃馒头
要求:
当做馒头的时候,不能吃馒头
当吃馒头的时候不能做馒头
馒头上限30个
在吃馒头的时候不能出现一个馒头被多个老和尚吃
不能出现吃的时候老和尚吃出异常来(如一个和尚永远也吃不到,或者一个和尚吃了一个不存在的馒头)
*/


import java.util.ArrayList;
import java.util.List;

public class ChiManTou  {
 static  class ManTou{
   private int num;
     public ManTou(int num) {
           this.num = num;
       }


       @Override
       public String toString() {
           return "第"+num+"个";
       }
   }
 
   public static List<ManTou> manTous=new ArrayList<>();
   public static void main(String[] args) {
       new HuoFu("大头和尚").start();
       new ChiHuo("白眉和尚").start();
       new ChiHuo("牛逼和尚").start();
       new ChiHuo("花和尚").start();
   }
   //伙夫
   static class HuoFu extends Thread{
       private String name;

       public HuoFu(String name) {
           this.name = name;
       }

       @Override
       public void run() {
           try {
               Thread.sleep(1000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           while (true) {
               synchronized ("a") {
                   System.out.println(name + "开始蒸馒头");
                   for (int i = 0; i < 30; i++) {
                       manTous.add(new ManTou(i + 1));
                       System.out.println("做完第" + (i + 1) + "个码头");
                       try {
                           Thread.sleep(100);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
                   System.out.println("馒头做好了,开始吃吧");
                   "a".notifyAll();
               }
               synchronized ("b") {
                   try {
                       System.out.println("我先去睡一会,没了叫我");
                       "b".wait();
                       System.out.println("我知道了开始做去");
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
       }
   }
   //吃货
   static class ChiHuo extends Thread{
       private String name;

       public ChiHuo(String name) {
           this.name = name;
       }

       @Override
       public void run() {
            synchronized ("a"){
                try {
                    "a".wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
           ManTou m=null;
           while (true) {
               synchronized ("a") {
                   System.out.println(name + "开始吃馒头");

                   if (manTous.size() != 0) {
                       m = manTous.remove(0);
                   } else {
                       System.out.println(name + "发现没有馒头,叫醒火夫做馒头");
                       synchronized ("b"){
                           "b".notify();
                       }
                       try {
                           System.out.println("我等着呢,完事后叫我吃");
                           "a".wait();
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }
               if (m != null) {
                   System.out.println(name + "消化馒头"+m.toString());
                   int rd = (int) (Math.random() * 2000 + 1000);
                   m = null;
                   try {
                       Thread.sleep(rd);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
       }
   }
}

谢谢!




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值