1

我正在编写一个分配方案程序,当用户单击时创建“行星”,并在单击复选框时启动/停止行星相互绕行。我们应该用一个线程来实现它。但是,当我单击复选框时,线程挂起似乎不起作用,但恢复却起作用。

谢谢你的尽心帮助!这是代码:

#lang racket

(require racket/gui)
(require racket/block)

;; Small 2d vector library for the Newtonian physics
(define (x v) (vector-ref v 0))
(define (y v) (vector-ref v 1))
(define (x! v value) (vector-set! v 0 value))
(define (y! v value) (vector-set! v 1 value))
(define (v* v value) (vector-map (lambda (x) (* x value)) v))
(define (v+ v w) (vector-map + v w))
(define (v- v w) (vector-map - v w))
(define (v-zero! v) (vector-map! (lambda (x) 0) v))
(define (v-dot v w) (let ((vw (vector-map * v w))) (+ (x vw) (y vw))))
(define (v-mag v) (sqrt (v-dot v v)))
(define sem (make-semaphore))


;; Planet object
(define planet%
  (class object%
(public m p v calculate-force move draw)
(init-field (mass 1)
            (position (vector 0 0 ))
            (velocity (vector 0 0 ))
            (force (vector 0 0 )))
(define (m) mass)
(define (p) position)
(define (v) velocity)
;; Use Newton's law of gravitation.
;; I assume the gravitational constant is one
(define (calculate-force planet-list)
  (v-zero! force)
  (for-each (lambda (other-planet)
              (when (not (equal? this other-planet))
                (let* ((direction (v- (send other-planet p) position))
                       (dist (max 1 (v-mag direction)))
                       (other-mass (send other-planet m))
                       (new-force (v* direction (/ (* mass other-mass) (* dist dist))))
                      )
                  (vector-map! + force new-force))))
            planet-list)
  )
;; Simple Euler integration of acceleration and velocity
(define (move) 
  (let ((acc (v* force (/ 1.0 mass))))
    (vector-map! + velocity acc)
    (vector-map! + position velocity)))
;; Draw a circle 
(define (draw dc) 
  (send dc set-brush brush)
  (send dc set-pen pen)
  (send dc draw-ellipse (x position) (y position) radius radius ))
;; Initialize to random velocity, mass, and color
(x! velocity (random))
(y! velocity (random))
(set! mass (+ 1 (* 10 (random))))
(define radius (* 5 (sqrt mass)))
(define color 
  (let* ((r (random))
         (b (real->floating-point-bytes r 4)))
    (make-object color% (bytes-ref b 0) (bytes-ref b 1) (bytes-ref b 2) )))
(define brush (make-object brush% color))
(define pen (make-object pen% color))
;; Don't forget the super-new!
(super-new)
))
;; Abstract the list-handling for a list of planets
(define planet-list%
  (class object%
(public add-planet calculate-force move draw)
(init-field (planets '()))
(define (add-planet planet)
  (set! planets (cons planet planets)))
(define (calculate-force)
  (for-each (lambda (planet)
              (send planet calculate-force planets))
            planets))
(define (move)
  (for-each (lambda (planet)
              (send planet move))
            planets))
(define (draw dc)
  (for-each (lambda (planet)
              (send planet draw dc))
            planets))
(super-new)
)
  )
(define planet-list (new planet-list%))

;; The GUI
(define frame (new frame% 
               (label "Planets")
               (min-width 120)
               (min-height 80)
               ))
(send frame create-status-line)
(send frame show #t)

(define h-panel
  (new horizontal-panel%
   (parent frame)
   (stretchable-height #f)
   (style '(border))
   (border 2)))

(define run-checkbox
  (new check-box%
   (parent h-panel)
   (label "Run animation")
   (callback
    (lambda (button event)
      (cond [(send run-checkbox get-value)(thread-resume (thread-a))]
            [(not (send run-checkbox get-value)) (thread-suspend (thread-a))]
   )))
    ))

(define my-canvas%
  (class canvas%
(override on-paint on-event)

(define (on-paint)
  (let ((dc (send this get-dc))
        (w (send this get-width))
        (h (send this get-height)))
    (send dc clear)
    (send planet-list draw dc)
    ))
(define (on-event event)
  (when (send event button-down?)
    (let ((x (send event get-x))
          (y (send event get-y)))
      (send frame set-status-text (format "Mouse at ~a ~a" x y))
      (send planet-list add-planet (new planet% (position (vector x y))))

      (send this refresh)))
  )
(super-new)
(send (send this get-dc) set-background (make-object color% 8 8 64))
))

(define canvas
  (new my-canvas%
   (parent frame)
   (style '(border))
   (min-width 640)
   (min-height 480)))

;; planet animator
(define thread-a (lambda ()
(let loop ()
  (sleep/yield .1)
(send planet-list calculate-force)
(send planet-list move)
(send canvas refresh)
  (loop))))

; this creates the thread-a and starts the program

(thread-suspend (thread thread-a))
4

1 回答 1

3

你能像现在这样工作真是太神奇了。

问题是那thread-a不是线程。它不是产生线程的函数。这是一个永远运行的函数,可以移动行星并更新画布。

(thread-suspend (thread-a))因此,例如,当您的复选框的回调确实发生时,thread-suspend从未真正发生过。调用thread-a刚刚开始运行并且永远不会返回。

GUI 没有锁定的原因(如果事件回调没有返回通常会锁定)是thread-a定期调用sleep/yield,这允许 GUI 事件循环处理更多事件。(这就是为什么我说代码很神奇。)

修复方法是定义thread-a为线程本身:

(define thread-a
  (thread
    (lambda ()
      (let loop () ....))))
(thread-suspend thread-a)

并将其他引用从更改(thread-a)为 just thread-a

于 2013-01-26T22:34:28.173 回答