关于线程

----------------------        http://edu.csdn.net/heima android培训               http://edu.csdn.net/heima     java培训      期待与您交流! ----------------

一、通过实现Runnable接口可以创建线程,调用该线程时,会自动执行run()方法,弄了个例子:



Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public class Test1 implements Runnable {

    
public void run() {
        System.out.println(
123);
        
try {
            Thread.sleep(
3000);
        } 
catch (InterruptedException e) {
        }
        System.out.println(
456);
    }

    
public static void main(String[] args) {
        Runnable r 
= new Test1();
        Thread thread 
= new Thread(r);
        thread.start();
    }

}

执行后,控制台先输出123,过3秒后输出456

 

二、改写上面的例子,执行两个线程,并且给线程指定名字:



Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public class Test2 implements Runnable {

    
public void run() {
        System.out.println(Thread.currentThread().getName());
    }
    
    
public static void main(String[] args) {
        Runnable r 
= new Test2();
        Thread thread1 
= new Thread(r,"name1");
        thread1.start();
        
        Thread thread2 
= new Thread(r,"name2");
        thread2.start();        
    }

}

这个例子会分别输出name1,name2

 

三、上面的thread1,thread2是并行执行的,再改一下代码,让多线程之间同步执行,所谓的同步执行,就是在thread1执行时,阻塞thread2的执行,等thread1执行完后,再执行thread2:



Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public class Test3 implements Runnable {

    
public void execute(String threadName) {
        
synchronized (this) {
            System.out.println(threadName 
+ " start");
            
try {
                Thread.sleep(
1000);
            } 
catch (InterruptedException e) {
            }
            System.out.println(threadName 
+ " end");
        }
    }

    
public void run() {
        String threadName 
= Thread.currentThread().getName();
        execute(threadName);
    }

    
public static void main(String[] args) {
        Runnable r 
= new Test3();
        Thread thread1 
= new Thread(r, "name1");
        thread1.start();

        Thread thread2 
= new Thread(r, "name2");
        thread2.start();
    }
}

可以把synchronized (this) {} 去掉,比较两次执行的效果

四、还可以通过设定线程的优先级,设置线程的执行顺序



Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->public class Test3 implements Runnable {

    
public void execute(String threadName) {
        
synchronized (this) {
            System.out.println(threadName 
+ " start");
            
try {
                Thread.sleep(
1000);
            } 
catch (InterruptedException e) {
            }
            System.out.println(threadName 
+ " end");
        }
    }

    
public void run() {
        String threadName 
= Thread.currentThread().getName();
        execute(threadName);
    }

    
public static void main(String[] args) {
        Runnable r 
= new Test3();
        Thread thread1 
= new Thread(r, "name1");
        thread1.setPriority(
3);

        Thread thread2 
= new Thread(r, "name2");
        thread1.setPriority(
2);
        
        thread1.start();
        thread2.start();
    }
好想快点进黑马,快疯了  啊啊啊

---------------------- http://edu.csdn.net/heima  android培训                 http://edu.csdn.net/heima    java培训    期待与您交流! -------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是一个关于创建线程的 Python 代码示例: ``` import threading class MyThread(threading.Thread): def __init__(self, thread_id, name, counter): threading.Thread.__init__(self) self.thread_id = thread_id self.name = name self.counter = counter def run(self): print("开始线程:" + self.name) print_time(self.name, self.counter, 5) print("退出线程:" + self.name) def print_time(thread_name, delay, counter): while counter: time.sleep(delay) print("%s: %s" % (thread_name, time.ctime(time.time()))) counter -= 1 thread1 = MyThread(1, "Thread-1", 1) thread2 = MyThread(2, "Thread-2", 2) thread1.start() thread2.start() thread1.join() thread2.join() print("退出主线程") ``` 在这段代码中,我们定义了一个 `MyThread` 类,继承自 Python 中的 `threading.Thread` 类。在这个类中,我们重写了 `run` 方法,来执行线程的任务。接着,我们创建了两个线程对象,并启动它们。最后,使用 `join` 方法等待两个线程执行完毕。 ### 回答2: 在Python中,可以使用`threading`模块来创建和管理线程。下面是一个简单的示例代码,使用Python的类来实现线程: ```python import threading import time class MyThread(threading.Thread): def __init__(self, name): threading.Thread.__init__(self) self.name = name def run(self): print(f"线程 {self.name} 启动") # 执行线程任务 for i in range(5): print(f"线程 {self.name}: 执行任务 {i}") time.sleep(1) print(f"线程 {self.name} 结束") # 创建两个线程对象 thread1 = MyThread("线程1") thread2 = MyThread("线程2") # 启动线程 thread1.start() thread2.start() # 等待线程完成 thread1.join() thread2.join() print("主线程结束") ``` 这个示例代码定义了一个`MyThread`类,继承自`threading.Thread`类,重写了`run`方法,该方法是线程的入口点。在`run`方法中,我们定义了线程要执行的任务。 然后,我们创建了两个`MyThread`对象,分别命名为"线程1"和"线程2"。接着,我们分别启动这两个线程,并使用`join`方法等待线程执行完毕。 最后,主线程打印出 "主线程结束",表示主线程的执行也结束了。 这段代码演示了如何使用Python的类来创建和管理线程。每个线程执行自己的任务,而不会干扰其他线程的执行。使用类可以方便地管理线程,并提供更好的代码组织和可维护性。 ### 回答3: 下面是一个使用Python类编写的关于线程的代码示例: ```python import threading import time # 创建一个继承自Thread类的自定义线程类 class MyThread(threading.Thread): def __init__(self, thread_id, name): threading.Thread.__init__(self) self.thread_id = thread_id self.name = name # 重写run方法,线程启动后执行该方法 def run(self): print("线程 " + self.name + " 正在运行...") time.sleep(2) # 线程休眠2秒 print("线程 " + self.name + " 运行结束.") # 创建线程对象 thread1 = MyThread(1, "Thread 1") thread2 = MyThread(2, "Thread 2") # 启动线程 thread1.start() thread2.start() # 等待线程执行完毕 thread1.join() thread2.join() print("所有线程执行完毕.") ``` 以上代码定义了一个自定义的线程类`MyThread`,该类继承自`Thread`类,并重写了`run`方法,用于线程的实际操作。在`run`方法中,我们模拟了线程运行2秒的操作。然后,创建了两个线程对象`thread1`和`thread2`,并通过调用`start`方法启动它们。最后,调用`join`方法等待线程执行完毕,并输出"所有线程执行完毕"的提示。 这段代码展示了如何使用Python的类编写多线程程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值