可以同時運行。另外,你的類名最好是大寫;想要使用線程類,最好是實現Runnable介面。
『貳』 VB怎麼實現穩定的多線程程序
相關代碼如下:
Private
Const
MyEvent
As
String
=
"m5home"
Dim
hEvent
As
Long
Sub
Main()
If
GetEventHandle
=
0
Then
'由於每個新對象的建立,都要執行Main()過程,因此使用事件對象來進行判斷.
hEvent
=
CreateEvent(0&,
False,
False,
MyEvent)
'在單元線程下,模塊里的全局變數已經沒有用了.frmMain.Show
End
If
End
Sub
Private
Function
GetEventHandle()
As
Long
GetEventHandle
=
OpenEvent(EVENT_ALL_ACCESS,
False,
MyEvent)
Call
CloseHandle(GetEventHandle)
End
Function
Public
Function
Quit()
Call
CloseHandle(hEvent)
End
Function
Private
Const
MyEvent
As
String
=
"m5home"
Dim
hEvent
As
Long
Sub
Main()
If
GetEventHandle
=
0
Then
'由於每個新對象的建立,都要執行Main()過程,因此使用事件對象來進行判斷.
hEvent
=
CreateEvent(0&,
False,
False,
MyEvent)
'在單元線程下,模塊里的全局變數已經沒有用了.frmMain.Show
End
If
End
Sub
Private
Function
GetEventHandle()
As
Long
GetEventHandle
=
OpenEvent(EVENT_ALL_ACCESS,
False,
MyEvent)
Call
CloseHandle(GetEventHandle)
End
Function
Public
Function
Quit()
Call
CloseHandle(hEvent)
End
Function
由於事件對象是系統范圍的,因此可以比較完美的完成這個工作.
同時事件對象在進程消失後,會自動釋放,也方便:)
示例代碼可以生成一個新的單元線程,並在這個線程裡面顯示一個窗體,窗體進行一個大循環.
循環內沒有放入DoEvents函數,因此會造成循環所在窗體無響應.
而此時主窗體不受影響.
『叄』 1. 寫出用Java編寫多線程程序的兩種常用方法
1、繼承Thread,然後生成對象
2、用類A實現runable介面,然後用你實現runnable的類A,生成Thread對象 Thread(A對象);
API 上說明如下:
創建新執行線程有兩種方法。一種方法是將類聲明為 Thread 的子類。該子類應重寫 Thread 類的
run 方法。接下來可以分配並啟動該子類的實例。例如,計算大於某一規定值的質數的線程可以寫成:
classPrimeThreadextendsThread{
longminPrime;
PrimeThread(longminPrime){
this.minPrime=minPrime;
}
publicvoidrun(){
//
...
}
}
然後,下列代碼會創建並啟動一個線程:
PrimeThreadp=newPrimeThread(143);
p.start();
創建線程的另一種方法是聲明實現 Runnable 介面的類。該類然後實現 run
方法。然後可以分配該類的實例,在創建 Thread 時作為一個參數來傳遞並啟動。採用這種風格的同一個例子如下所示:
implementsRunnable{
longminPrime;
PrimeRun(longminPrime){
this.minPrime=minPrime;
}
publicvoidrun(){
//
...
}
}
然後,下列代碼會創建並啟動一個線程:
rimeRunp=newPrimeRun(143);
newThread(p).start();
『肆』 java多線程編程代碼如下,輸出結果如下:
首先,你同步的是具體的某個Test實例, 對於那個實例來說,實際上只有一個線程訪問了那個代碼塊,但是sum和other卻是多個線程同時去進行訪問,實際上這是不安全的,如果你想實現每次都輸出10000的效果,那麼正確的應該是在Test.class上加鎖,而不是獲取Test實例的鎖,修改後的代碼如下:
publicclassTestextendsThread{
publicstaticintsum=10000;
publicstaticintother=0;
publicvoidgetMoney(){
synchronized(Test.class){
System.out.println(Thread.currentThread().getName()+"開始執行");
sum=sum-100;
System.out.println("sum-100");
other=other+100;
System.out.println("other+100");
System.out.println(sum+other);
System.out.println(Thread.currentThread().getName()+"執行完成");
}
}
publicvoidrun(){
getMoney();
}
publicstaticvoidmain(String[]agrs){
Threadt[]=newThread[10];
for(inti=0;i<=9;i++){
t[i]=newTest();
t[i].start();
}
}
}
// 上面代碼能得到你的結果
『伍』 什麼是Java多線程編程
一、 什麼是多線程:
我們現在所使用操作系統都是多任務操作系統(早期使用的DOS操作系統為單任務操作系統),多任務操作指在同一時刻可以同時做多件事(可以同時執行多個程序)。
多進程:每個程序都是一個進程,在操作系統中可以同時執行多個程序,多進程的目的是為了有效的使用CPU資源,每開一個進程系統要為該進程分配相關的系統資源(內存資源)
多線程:線程是進程內部比進程更小的執行單元(執行流|程序片段),每個線程完成一個任務,每個進程內部包含了多個線程每個線程做自己的事情,在進程中的所有線程共享該進程的資源;
主線程:在進程中至少存在一個主線程,其他子線程都由主線程開啟,主線程不一定在其他線程結束後結束,有可能在其他線程結束前結束。Java中的主線程是main線程,是Java的main函數;
二、 Java中實現多線程的方式:
繼承Thread類來實現多線程:
當我們自定義的類繼承Thread類後,該類就為一個線程類,該類為一個獨立的執行單元,線程代碼必須編寫在run()方法中,run方法是由Thread類定義,我們自己寫的線程類必須重寫run方法。
run方法中定義的代碼為線程代碼,但run方法不能直接調用,如果直接調用並沒有開啟新的線程而是將run方法交給調用的線程執行
要開啟新的線程需要調用Thread類的start()方法,該方法自動開啟一個新的線程並自動執行run方法中的內容
java多線程的啟動順序不一定是線程執行的順序,各個線程之間是搶佔CPU資源執行的,所有有可能出現與啟動順序不一致的情況。
CPU的調用策略:
如何使用CPU資源是由操作系統來決定的,但操作系統只能決定CPU的使用策略不能控制實際獲得CPU執行權的程序。
線程執行有兩種方式:
1.搶占式:
目前PC機中使用最多的一種方式,線程搶佔CPU的執行權,當一個線程搶到CPU的資源後並不是一直執行到此線程執行結束,而是執行一個時間片後讓出CPU資源,此時同其他線程再次搶佔CPU資源獲得執行權。
2.輪循式;
每個線程執行固定的時間片後讓出CPU資源,以此循環執行每個線程執行相同的時間片後讓出CPU資源交給下一個線程執行。
希望對您有所幫助!~
『陸』 c# 多線程編程
稍作修改,運行代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace TestFormsApp
{
public partial class Form1 : Form
{
Serial serial = new Serial();//自定義類Serial
CheckIsOver ck = new CheckIsOver();//自定義CheckIsOver
public Form1()
{
InitializeComponent();
serial.Starting = true;
Thread derThread = new Thread(serial.OpenSerial);
derThread.Start();
ck.Starting = true;
Thread derThread1 = new Thread(ck.main1);
derThread1.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 線程退出
serial.Starting = false;
ck.Starting = false;
}
}
}
在輸出窗口中可以看到線程運行情況:
Serial's thread:0
CheckIsOver's thread:0
CheckIsOver's thread:1
Serial's thread:1
Serial's thread:2
CheckIsOver's thread:2
Serial's thread:3
CheckIsOver's thread:3
線程 0xe48 已退出,返回值為 0 (0x0)。
線程 0x380 已退出,返回值為 0 (0x0)。
樓主有郵箱發給我,我把剛做的例子發給你
『柒』 求一個Java多線程程序代碼的注釋
多線程實現方式有兩種:繼承Thread類和實現Runnable介面,二者區別及示例如下:
1.實現方式不同及可擴展性
Thread 採用單繼承的方式,繼承Thread的類不可再繼承其它類。
Runnable介面採用實現介面的方式,可再繼承其它類。
2.方法執行體中 run( ),調用getName方法不同。
Thread可直接通過成員方法getName()獲取當前運行時的線程名稱。
Runnable中 必須通過 Thread.currentThread().getName()方法得到。
3.啟動線程為就緒狀態的方法不同。
Thread類 通過 new ThreadTest().start()的方法可以同時啟動多個線程 其線程名默認為 Thread-0/1/2。
Runnable 需要通過 RunableTest rt= new RunableTest();
new Thread(rt,"線程0" ).start();
new Thread(rt,"線程1" ).start();
4.是否共享內存資源。
Thread 多個線程之間不共享內存資源 各自獨立
Runnable 多個線程共享同一個內存資源。
方式一:
public class ThreadTest extends Thread {
private int i ;
public void run(){
for(;i <100;i++){
System. out.println("run:" +getName()+" "+ i);
}
}
public static void main(String[] args) {
for(int i=0;i<100;i++){
System.out.println("main:" +Thread.currentThread ().getName()+" " +i);
if(i==20){
new ThreadTest().start();
new ThreadTest().start();
}
}
}
}
方式二
public class RunableTest implements Runnable{
private int i ;
@Override
public void run(){
for(;i <100;i++){
String name=Thread. currentThread().getName();
System. out.println("run:" +name+" "+ i);
}
}
public static void main(String[] args) throws InterruptedException {
for(int i=0;i<100;i++){
System. out.println(Thread.currentThread().getName()+ " "+i);
if(i==20){
RunableTest rt= new RunableTest();
new Thread(rt,"線程0" ).start();
new Thread(rt,"線程1" ).start();
}
}
}
}
『捌』 求JAVA多線程編程代碼
測試過了,沒問題。基本思路,實例化一個橋類,誰得到橋的可用標志誰過橋。
我第一個看到這個100分的,說實話,知道你是個學生要代碼而已,線程類好久沒練手了,練習一下而已,否則真不會給你寫代碼。因為我讀書的時候也發過類似的求助,知道什麼感受。不懂的時候真的沒辦法,所以告訴你思路。
package cn.thread;
public class Through_out_bridge {
public static void main(String[] args) {
Bridge b = Bridge.getInstance();//實例化橋
//實例化左端9個人,此時所有人都不能過橋,橋的可以狀態標志為不可以用
for (int i = 1; i <= 9; i++) {
Thread t = new Thread(new Person(false, i, b));
t.start();
}
//實例化右端12個人,此時所有人都不能過橋,橋的可以狀態標志為不可以用
for( int i=1 ;i<=12;i++)
{
Thread t = new Thread(new Person(true,i,b));
t.start();
}
//橋的可用狀態給左端第10個人,可以自定義給誰
b.state = true;
Thread t = new Thread(new Person(false, 10, b));
}
}
class Person implements Runnable {
public Bridge bridge;//橋
private String hand;//在橋哪一端
int No;//序號
public Person(boolean side, int i, Bridge bridge) {
this.No = i;
this.bridge = bridge;
if(bridge.state)
{
System.out.println(i+"已經過橋。");
}
if (side) {
this.hand = new String("右");
} else {
this.hand = new String("左");
}
}
//過橋方法
public synchronized void through() throws InterruptedException {
if (bridge.state) {
System.out.println(hand+"邊第"+No + "在過橋");
bridge.open( No);
} else {
bridge.lock(No);
}
}
public void run() {
try {
Thread.sleep(1000);
// System.out.println(No+hand+" 邊已經過橋!");
through();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Bridge {
//可用狀態判斷true:可用
public boolean state = false;
//自行實例化
public static Bridge getInstance() {
return new Bridge();
}
public synchronized void open(int i) throws InterruptedException {
if (state) {
Thread.sleep(1000);
this.state=true;
notify();
}
}
public synchronized void lock(int i) throws InterruptedException {
if (!state) {
this.state=false;
System.out.println(i + " 在等待.");
wait();
}
}
}
『玖』 求java多線程編程代碼,要有注釋,非誠勿擾~~~做出後分不是問題~~
這個是實驗時候做的例子 希望對你有用 包括實現多線程的兩種方法
一、用創建Thread 類的子類的方法實現多線程一
源程序:
class FruitThread extends Thread{
public FruitThread(String str){
super(str);
}
public void run(){ //線程的run方法
for(int i=0;i<5;i++){
System.out.println(i+" "+getName());
try{
sleep((int)(Math.random()*1000));//等待
}
catch(InterruptedException e){
}
}
}
}
public class TwoFtuit{
public static void main(String[] a){
new FruitThread("蘋果").start(); //第一個線程
new FruitThread("梨").start(); //第二個線程
}
}
實驗結果:
E:\>javac TwoFtuit.java
E:\>java TwoFtuit
0 蘋果
0 梨
1 蘋果
2 蘋果
1 梨
3 蘋果
2 梨
4 蘋果
3 梨
4 梨
二、用實現Runnable 介面的方法實現多線程
源程序:
class outputClass implements Runnable{
String name;
outputClass(String s){
name =s;
}
public void run(){
for(int i=0;i<3;i++){
System.out.println(name);
Thread.yield();
}
}
}
public class runThreads{
public static void main(String[] a){
outputClass out1=new outputClass("Thread1");
outputClass out2=new outputClass("Thread2");
Thread t1=new Thread(out1);
Thread t2=new Thread(out2);
t1.start();
t2.start();
}
}
實驗結果:
E:\java>javac runThreads.java
E:\java>java runThreads
Thread1
Thread2
Thread1
Thread2
Thread1
Thread2
『拾』 Linux多線程編程
程序代碼test.c共兩個線程,一個主線程,一個讀緩存區的線程:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char globe_buffer[100];
void *read_buffer_thread(void *arg); //這里先聲明一下讀緩存的線程,具體實現寫在後面了
int main()
{
int res,i;
pthread_t read_thread;
for(i=0;i<20;i++)
globe_buffer[i]=i;
printf("\nTest thread : write buffer finish\n");
sleep(3);\\這里的3秒是多餘,可以不要。
res = pthread_create(&read_thread, NULL, read_buffer_thread, NULL);
if (res != 0)
{
printf("Read Thread creat Error!");
exit(0);
}
sleep(1);
printf("waiting for read thread to finish...\n");
res = pthread_join(read_thread, NULL);
if (res != 0)
{
printf("read thread join failed!\n");
exit(0);
}
printf("read thread test OK, have fun!! exit ByeBye\n");
return 0;
}
void *read_buffer_thread(void *arg)
{
int i,x;
printf("Read buffer thread read data : \n");
for(i=0;i<20;i++)
{
x=globe_buffer[i];
printf("%d ",x);
globe_buffer[i]=0;//清空
}
printf("\nread over\n");
}
---------------------------------------------------------------------------------
以上程序編譯:
gcc -D_REENTRANT test.c -o test.o –lpthread
運行這個程序:
$ ./test.o: