首页 \ 问答 \ Threadlocal值iList排序(Threadlocal values iList ordering)

Threadlocal值iList排序(Threadlocal values iList ordering)

嗨,我有一个创建2个线程的示例。 我的问题是,当我输出值时,它总是在999之前打印1000.是否可以在1000之前打印999.只是想知道它们是如何订购的?

 static void Main(string[] args)
    {
        ThreadLocal<int> field = new ThreadLocal<int>(() => 0, true);



        Thread firstThread = new Thread(new ThreadStart(() =>
        {

            field.Value = 999;
        }));
        Thread secondThread = new Thread(new ThreadStart(() =>
        {

            field.Value = 1000;
        }));


        firstThread.Start();
        secondThread.Start();


        firstThread.Join();
        secondThread.Join();

        IList<int> valueList = field.Values;

        foreach (int arr in valueList)
            Console.WriteLine(arr);


        Console.Read();
    }

Hi i have an example which creates 2 threads. My question is when i output the values it always prints 1000 before 999. Is it possible to print 999 before 1000. Just want to know how are they ordered?

 static void Main(string[] args)
    {
        ThreadLocal<int> field = new ThreadLocal<int>(() => 0, true);



        Thread firstThread = new Thread(new ThreadStart(() =>
        {

            field.Value = 999;
        }));
        Thread secondThread = new Thread(new ThreadStart(() =>
        {

            field.Value = 1000;
        }));


        firstThread.Start();
        secondThread.Start();


        firstThread.Join();
        secondThread.Join();

        IList<int> valueList = field.Values;

        foreach (int arr in valueList)
            Console.WriteLine(arr);


        Console.Read();
    }

原文:https://stackoverflow.com/questions/33978889
更新时间:2023-04-10 17:04

最满意答案

这真的不是很难......但是我在快速搜索中找到的最好的示例代码也是一个可用的库,可以为你工作: spritesheet ,直接来自pygame wiki。

所以,你可以从使用它开始。 我会给你一个根据你的用例量身定做的例子,但是你没有告诉我们你的代码是什么样的或者你想做什么,所以我不可能给你比那个页面更好的东西给你,所以:

import spritesheet
...
ss = spritesheet.spritesheet('somespritesheet.png')
# Sprite is 16x16 pixels at location 0,0 in the file...
image = ss.image_at((0, 0, 16, 16))
images = []
# Load two images into an array, their transparent bit is (255, 255, 255)
images = ss.images_at((0, 0, 16, 16),(17, 0, 16,16), colorkey=(255, 255, 255))
…

同时,您可以阅读spritesheet类中的(非常简单的)代码以了解它的工作原理。


It really isn't very hard to do… but the best sample code I found in a quick search is also a usable library that does the work for you: spritesheet, right from the pygame wiki.

So, you can start off by just using that. I'd give you an example tailored to your use case, but you haven't given us any idea what your code looks like or what you want to do, so I can't possibly give you anything better than is already on that page, so:

import spritesheet
...
ss = spritesheet.spritesheet('somespritesheet.png')
# Sprite is 16x16 pixels at location 0,0 in the file...
image = ss.image_at((0, 0, 16, 16))
images = []
# Load two images into an array, their transparent bit is (255, 255, 255)
images = ss.images_at((0, 0, 16, 16),(17, 0, 16,16), colorkey=(255, 255, 255))
…

Meanwhile, you can read the (very simple) code in that spritesheet class to understand how it works.

相关问答

更多
  • 全名是pygame.sprite.Sprite 。 如果你使用import pygame.sprite as Sprite那么正确的名称是player(Sprite.Sprite)而不是player(Sprite.sprite) - 在两个Sprite看到上面的S 至于我,最好使用class player(pygame.sprite.Sprite)和bullet(pygame.sprite.Sprite) - s一个sprite较低s和第二个Sprite上部S - 并且没有import pygame.spr ...
  • 这条线 self.rect = self.image.get_rect() 因为类Player没有名为image的属性会崩溃。 如果你想使用pygame的Sprite类,你应该遵循它的文档并添加一个image和一个rect属性: 可见游戏对象的基类。 派生类将要覆盖Sprite.update()并分配Sprite.image和Sprite.rect属性。 初始化程序可以接受要添加的任意数量的Group实例。 使用pygame的Sprite类的好处是你可以使用Group类及其子类轻松管理你的sprite。 ...
  • Mario是一个班级。 方法handle_keys(self)是一个实例方法 - 意味着它只能针对Mario实例调用。 (有可能有一个classmethod ,但这不是你想要的,因为你需要修改self 。) 在顶部你做了一个b = Mario([0, 800]) - 我将b改为mario到处都是c和ladder 。 mario = Mario([0, 800]) 然后你将使用mario.handle_keys()而不是mario.handle_keys() 。 更多背景: 当你调用mario.handl ...
  • 我认为问题是你有两个变量来保持位置。 但我没有检查 - 我更改了代码,使其更好地组织和问题消失。 #!/usr/bin/env python3 import pygame # --- constants --- WIDTH = 800 HEIGHT = 600 FPS = 30 WHITE = (225, 225, 225) # --- classes --- class Invader(): def __init__(self, x, y): self.image ...
  • 只需从Sprite继承你的中间类,然后继承它: class my_custom_class(pygame.sprite.Sprite): def common_physics_stuff() pass class ShipSprite(my_custom_class): ... 如果您想将“custom_class”事物添加到一个不必像Sprite一样的抽象类中,并且可以在其他上下文中使用,那么您也可以使用多重继承 - class my_custom_class(obje ...
  • 发现的第一个问题是你必须修改 pygame.image.load(Mario_sideways_sprite_2xL.png) 有类似的东西。 pygame.image.load("Mario_sideways_sprite_2xL.png") 除此之外,代码还有许多阻碍其工作的问题。 例如, 你没有实例化你的水管工班 。 class plumber(sprite)应该是plumber(Sprite) (还是更好的Plumber(Sprite) ) 你需要这样的东西: myplumber = Plumb ...
  • 它安静轻松, 使用Group.update()方法对sprite进行blitting。 all_sprites_list.draw(mainScreen) 不要将障碍添加到all_sprites_list。 然后他们永远不会在屏幕上搞砸了。 但是barrier_hit_list仍然可以用于碰撞。 its quiet easy, sprites were blitted with the Group.update() method. all_sprites_list.draw(mainScreen) Do ...
  • 这真的不是很难......但是我在快速搜索中找到的最好的示例代码也是一个可用的库,可以为你工作: spritesheet ,直接来自pygame wiki。 所以,你可以从使用它开始。 我会给你一个根据你的用例量身定做的例子,但是你没有告诉我们你的代码是什么样的或者你想做什么,所以我不可能给你比那个页面更好的东西给你,所以: import spritesheet ... ss = spritesheet.spritesheet('somespritesheet.png') # Sprite is 16x16 ...
  • 正如ecline6所述,在这一点上,鸟是你最不担心的。 考虑阅读这本书 .. 现在,首先让我们清理你的代码... import pygame import os # let's address the class a little later.. pygame.init() screen = pygame.display.set_mode((640, 400)) # you only need to call the following once,so pull them out of the whi ...
  • 好吧,所以pygame sprite对象是一个bass类,可以用来创建其他类,然后你可以用它来做碰撞检测等事情: class Player(pygame.Sprite): def __init__(self): self.image = pygame.image.load("player.png").convert_alpha() self.rect = self.image.get_rect() self.pos = self.rect.x, sel ...

相关文章

更多

最新问答

更多
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • 电脑高中毕业学习去哪里培训
  • 电脑系统专业就业状况如何啊?
  • IEnumerable linq表达式(IEnumerable linq expressions)
  • 如何在Spring测试中连接依赖关系(How to wire dependencies in Spring tests)
  • Solr可以在没有Lucene的情况下运行吗?(Can Solr run without Lucene?)
  • 如何保证Task在当前线程上同步运行?(How to guarantee that a Task runs synchronously on the current thread?)
  • 在保持每列的类的同时向数据框添加行(Adding row to data frame while maintaining the class of each column)
  • 的?(The ? marks in emacs/haskell and ghc mode)
  • 一个线程可以调用SuspendThread传递自己的线程ID吗?(Can a thread call SuspendThread passing its own thread ID?)
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • 悬停时的图像转换(Image transition on hover)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 没有JavaScript的复选框“关闭”值(Checkbox 'off' value without JavaScript)
  • java分布式框架有哪些
  • Python:填写表单并点击按钮确认[关闭](Python: fill out a form and confirm with a button click [closed])
  • PHP将文件链接到根文件目录(PHP Linking Files to Root File Directory)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 您是否必须为TFS(云)中的每个BUG创建一个TASK以跟踪时间?(Do you have to create a TASK for every BUG in TFS (Cloud) to track time?)
  • typoscript TMENU ATagParams小写(typoscript TMENU ATagParams lowercase)
  • 武陟会计培训类的学校哪个好点?
  • 从链接中删除文本修饰(Remove text decoration from links)