c#线程基本知识+多线程技术用于大量计算方面

本文介绍了Unity开发中如何利用多线程技术进行大量计算,避免主线程卡顿。主要内容包括开启线程、加锁同步、线程休眠以及通过启动额外线程来确保计算过程流畅。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

unity 13章 多线程技术与网络开发

13.1.1开启线程

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Thread thread = new Thread(run);
        thread.Start();
    }

    // Update is called once per frame
    void Update()
    {
                                                            
    }

    void run()
    {
        Debug.Log("开启线程");
    }
}

13.1.2加锁(同一时刻只能被一个线程访问或者修改)

获取锁才能操作数据,操作数据后释放数据

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;

public class NewBehaviourScript : MonoBehaviour
{
    public static Object o = new Object();
    public int n = 0;
    void Start()
    {
        Thread thread1 = new Thread(run1);
        Thread th
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值