当前位置:首页 > 代码 > 正文

多线程编程代码(多线程的代码)

admin 发布:2022-12-19 19:42 120


本篇文章给大家谈谈多线程编程代码,以及多线程的代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java多线程编程代码如下,输出结果如下:

首先,你同步的是具体的某个Test实例, 对于那个实例来说,实际上只有一个线程访问了那个代码块,但是sum和other却是多个线程同时去进行访问,实际上这是不安全的,如果你想实现每次都输出10000的效果,那么正确的应该是在Test.class上加锁,而不是获取Test实例的锁,修改后的代码如下:

public class Test extends Thread {

    public static int sum = 10000;

    public static int other = 0;

    public void getMoney() {

        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() + " 执行完成");

        }

    }

    public void run() {

        getMoney();

    }

    public static void main(String[] agrs) {

        Thread t[] = new Thread[10];

        for (int i = 0; i = 9; i++) {

            t[i] = new Test();

            t[i].start();

        }

    }

}

// 上面代码能得到你的结果

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)。

楼主有邮箱发给我,我把刚做的例子发给你

c语言如何编写一个简单的多线程程序?

这是一个多线程例子,里面只有两个线程,是生产者/消费者模式,已编译通过,注释很详细,

如下:

/* 以生产者和消费者模型问题来阐述Linux线程的控制和通信你

生产者线程将生产的产品送入缓冲区,消费者线程则从中取出产品。

缓冲区有N个,是一个环形的缓冲池。

*/

#include stdio.h

#include pthread.h

#define BUFFER_SIZE 16

struct prodcons

{

int buffer[BUFFER_SIZE];/*实际存放数据的数组*/

pthread_mutex_t lock;/*互斥体lock,用于对缓冲区的互斥操作*/

int readpos,writepos; /*读写指针*/

pthread_cond_t notempty;/*缓冲区非空的条件变量*/

pthread_cond_t notfull;/*缓冲区未满 的条件变量*/

};

/*初始化缓冲区*/

void pthread_init( struct prodcons *p)

{

pthread_mutex_init(p-lock,NULL);

pthread_cond_init(p-notempty,NULL);

pthread_cond_init(p-notfull,NULL);

p-readpos = 0;

p-writepos = 0;

}

/*将产品放入缓冲区,这里是存入一个整数*/

void put(struct prodcons *p,int data)

{

pthread_mutex_lock(p-lock);

/*等待缓冲区未满*/

if((p-writepos +1)%BUFFER_SIZE ==p-readpos)

{

pthread_cond_wait(p-notfull,p-lock);

}

p-buffer[p-writepos] =data;

p-writepos++;

if(p-writepos = BUFFER_SIZE)

p-writepos = 0;

pthread_cond_signal(p-notempty);

pthread_mutex_unlock(p-lock);

}

/*从缓冲区取出整数*/

int get(struct prodcons *p)

{

int data;

pthread_mutex_lock(p-lock);

/*等待缓冲区非空*/

if(p-writepos == p-readpos)

{

pthread_cond_wait(p-notempty ,p-lock);//非空就设置条件变量notempty

}

/*读书据,移动读指针*/

data = p-buffer[p-readpos];

p-readpos++;

if(p-readpos == BUFFER_SIZE)

p-readpos = 0;

/*设置缓冲区未满的条件变量*/

pthread_cond_signal(p-notfull);

pthread_mutex_unlock(p-lock);

return data;

}

/*测试:生产站线程将1 到1000的整数送入缓冲区,消费者线程从缓冲区中获取整数,两者都打印信息*/

#define OVER (-1)

struct prodcons buffer;

void *producer(void *data)

{

int n;

for( n=0;n1000;n++)

{

printf("%d ------\n",n);

put(buffer,n);

}

put(buffer,OVER);

return NULL;

}

void *consumer(void *data)

{

int d;

while(1)

{

d = get(buffer);

if(d == OVER)

break;

else

printf("-----%d\n",d);

}

return NULL;

}

int main()

{

pthread_t th_p,th_c;

void *retval;

pthread_init(buffer);

pthread_create(th_p,NULL,producer,0);

pthread_create(th_c,NULL,consumer,0);

/*等待两个线程结束*/

pthread_join(th_p, retval);

pthread_join(th_c,retval);

return 0;

}

Java关于io和多线程的编程题,求代码

package know;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

public class T20 {

 public static void main(String[] args) throws IOException {

  Scanner s=new Scanner(System.in);

  String username=null;

  String password=null;

  System.out.println("输入用户名:");

  while(true){

   if(username==null){

    username=s.next();

    System.out.println("输入密码:");

   }else{

    password=s.next();

   }

   if(password!=null){

    s.close();

    break;

   }

  }

  

  BufferedReader br=null;

  MapString, String map=new HashMapString, String();

  try{

   br=new BufferedReader(new InputStreamReader(new FileInputStream("d:/test.txt")));

   String temp=null;   

   while((temp=br.readLine())!=null){

    String[] ss=temp.split("=");

    map.put(ss[0], ss[1]);

   }

  }catch(IOException e){

   throw e;

  }finally{

   if(br!=null)

    br.close();

  }

  String u=map.get("userName");

  String p=map.get("password");

  if(u.equals(username)p.equals(password)){

   System.out.println("登录成功");

  }else{

   System.out.println("用户名或密码错误");

  }

 }

}

package know;

public class T21 {

 public static void main(String[] args) throws InterruptedException {

  String[] persons=new String[]{"甲","乙","丙","丁","午","己","庚","辛","壬","癸"};

  for(int i=0;ipersons.length;i++){

   System.out.println(persons[i]+"正在过山洞");

   Thread.sleep(5000);

  }

 }

}

最后一个百度搜一个就行了,肯定比我画的好

JAVA程序设计,多线程,求大神给一份可运行的代码

给你一个经典的例子。run里面放空循环来观察多线程是不合理的,空循环消耗时序极小,用sleep来间隔时间才是合理的。

class RunnableDemo implements Runnable {

   private Thread t;

   private String threadName;

   

   RunnableDemo( String name) {

      threadName = name;

      System.out.println("Creating " +  threadName );

   }

   

   public void run() {

      System.out.println("Running " +  threadName );

      try {

         for(int i = 4; i  0; i--) {

            System.out.println("Thread: " + threadName + ", " + i);

            // Let the thread sleep for a while.

            Thread.sleep(50);

         }

      }catch (InterruptedException e) {

         System.out.println("Thread " +  threadName + " interrupted.");

      }

      System.out.println("Thread " +  threadName + " exiting.");

   }

   

   public void start () {

      System.out.println("Starting " +  threadName );

      if (t == null) {

         t = new Thread (this, threadName);

         t.start ();

      }

   }

}

public class TestThread {

   public static void main(String args[]) {

      RunnableDemo R1 = new RunnableDemo( "Thread-1");

      R1.start();

      

      RunnableDemo R2 = new RunnableDemo( "Thread-2");

      R2.start();

   }   

}

多线程编程代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于多线程的代码、多线程编程代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/21102.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载